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 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 a function in same file as a class from within an instance of that class?

From Dev

Calling a Python function from another file

From Dev

Python Calling Function from Another File

From Dev

Calling a function from a HTML button

From Dev

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

From Dev

Calling a Python function from HTML is not working

From Dev

Calling Python function that requires libraries from another file

From Dev

Python calling function from other file

From Dev

Python calling a function from another function in a class

From Dev

Dynamic Function Calling From a Different File - Python

From Dev

Python calling a function from another file

From Dev

Python: calling function from imported 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

calling php function from html page

From Dev

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

From Dev

Python calling function in an function from another function

From Dev

Calling Javascript function from external file when loading html page

From Dev

Calling a Python function from HTML

From Dev

Issue when calling function from other Python file

From Dev

Calling a function from an external file in Python

From Dev

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

From Dev

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

From Dev

Calling function from another Python file

From Dev

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

From Dev

Calling function from HTML to typescript not waited to finish

From Dev

calling a func from a python file

Related Related

  1. 1

    Calling a function in a separate file in Python

  2. 2

    Calling a function in a separate file in Python

  3. 3

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

  4. 4

    Calling a Python function from another file

  5. 5

    Python Calling Function from Another File

  6. 6

    Calling a function from a HTML button

  7. 7

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

  8. 8

    Calling a Python function from HTML is not working

  9. 9

    Calling Python function that requires libraries from another file

  10. 10

    Python calling function from other file

  11. 11

    Python calling a function from another function in a class

  12. 12

    Dynamic Function Calling From a Different File - Python

  13. 13

    Python calling a function from another file

  14. 14

    Python: calling function from imported file

  15. 15

    Calling an output from a function from a different file?

  16. 16

    Calling a function from within a function in Python

  17. 17

    calling php function from html page

  18. 18

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

  19. 19

    Python calling function in an function from another function

  20. 20

    Calling Javascript function from external file when loading html page

  21. 21

    Calling a Python function from HTML

  22. 22

    Issue when calling function from other Python file

  23. 23

    Calling a function from an external file in Python

  24. 24

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

  25. 25

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

  26. 26

    Calling function from another Python file

  27. 27

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

  28. 28

    Calling function from HTML to typescript not waited to finish

  29. 29

    calling a func from a python file

HotTag

Archive