Calling a function from HTML to Python file

Ronaldo Lanhellas

I have a index.html page with a list of "cards", where each card have a "Click to Select" link.

When user click in this link i'd like to call a function in python to select this item, see:

def selectItem(request, item):
    #so something with this item

so, in my html pagE:

<div class="card-action">
                            <a href="{{ selectItem(myitem) }}">Selecionar</a>
                        </div>

This don't work. What is the right way to do it ?

Willem Van Onsem

You can not call a function like that. A browser requests data with an HTTP request, and the server answers with an (HTTP) response. Such requests have a URL, and Django can route the request - with the URL - to the right view that will calculate a response.

We thus need to construct a view that can then be called. Your call is already quite close:

# app/views.py

from django.http import HttpResponse

def select_item(request, item_id):
    # so something with this item_id
    # ...
    return HttpResponse()

Since most objects are not serializable (and usually you do not want that anyway, since it would expose a lot of (potentially sensitive) data to the user, we thus need to work with an id (an identifier that is for example stored in the database that corresponds to an object).

The response contains the data in the response. Frequently that is HTML code that is then rendered by the browser.

Now in urls.py, we can specify how the url looks like, for example:

# app/urls.py

from django.urls import path
from app.views import select_item

urlpatterns = [
    path('select_item/<int:item_id>/', select_item, name='select_item_view'),
    # ...
]

The urlpatterns need to be included in the root urlpatterns (in the project root).

Now in the HTML template, we can generate the URL that matches with this view, something similar to:

<div class="card-action">
  <a href="{% url 'select_item_view' item_id=myitem.id %}">Selecionar</a>
</div>

Django will then make sure that the href points to an URL that refers to the select_item view with the correct parameter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Python function from HTML

From Dev

Calling a Python function from HTML is not working

From Dev

Python: calling function from imported file

From Dev

Calling a Python function from another file

From Dev

Python Calling Function from Another File

From Dev

Calling a function from an external file in Python

From Dev

Python calling function from other file

From Dev

Python calling a function from another file

From Dev

Dynamic Function Calling From a Different File - Python

From Dev

Calling function from another Python file

From Dev

Calling a Javascript function from an external js file in the html file

From Dev

Calling/Invoking a Javascript function from python a flask function within html

From Dev

Calling a Python function from another file and using the DF of that function

From Dev

Calling Javascript function from external file when loading html page

From Dev

Calling Python function that requires libraries from another file

From Dev

Python -- calling a function in same file as a class from within an instance of that class?

From Dev

Issue when calling function from other Python file

From Dev

Calling a function from a HTML button

From Java

Calling a function in a separate file in Python

From Java

Calling a function in a separate file in Python

From Dev

Python calling function in an function from another function

From Dev

Calling a function in a .cpp file from a .c file

From Dev

calling a func from a python file

From Dev

Calling an output from a function from a different file?

From Dev

Calling a function from within a function in Python

From Dev

Python calling a function from another function in a class

From Dev

Why after calling a function from an imported python file within a file the PyQt app stops responding?

From Dev

calling php function from html page

From Dev

Calling function from HTML to typescript not waited to finish

Related Related

  1. 1

    Calling a Python function from HTML

  2. 2

    Calling a Python function from HTML is not working

  3. 3

    Python: calling function from imported file

  4. 4

    Calling a Python function from another file

  5. 5

    Python Calling Function from Another File

  6. 6

    Calling a function from an external file in Python

  7. 7

    Python calling function from other file

  8. 8

    Python calling a function from another file

  9. 9

    Dynamic Function Calling From a Different File - Python

  10. 10

    Calling function from another Python file

  11. 11

    Calling a Javascript function from an external js file in the html file

  12. 12

    Calling/Invoking a Javascript function from python a flask function within html

  13. 13

    Calling a Python function from another file and using the DF of that function

  14. 14

    Calling Javascript function from external file when loading html page

  15. 15

    Calling Python function that requires libraries from another file

  16. 16

    Python -- calling a function in same file as a class from within an instance of that class?

  17. 17

    Issue when calling function from other Python file

  18. 18

    Calling a function from a HTML button

  19. 19

    Calling a function in a separate file in Python

  20. 20

    Calling a function in a separate file in Python

  21. 21

    Python calling function in an function from another function

  22. 22

    Calling a function in a .cpp file from a .c file

  23. 23

    calling a func from a python file

  24. 24

    Calling an output from a function from a different file?

  25. 25

    Calling a function from within a function in Python

  26. 26

    Python calling a function from another function in a class

  27. 27

    Why after calling a function from an imported python file within a file the PyQt app stops responding?

  28. 28

    calling php function from html page

  29. 29

    Calling function from HTML to typescript not waited to finish

HotTag

Archive