Passing variable through Django template url tag

Ibrahim Fakih

I am trying to pass a variable through the Django template url tag with little success.

My path in the urls.py:

path("viewListing/<int:listingID>", views.bookListing, name="viewListing")

In the template, I am using javascript to generate the listingID with a for loop.

for(j=0; j<5; j++){
  var thisID = (data[j].id)
  imgA.href = "{% url 'viewListing' thisID %}";
}

However, I am getting this error

Reverse for 'viewListing' with arguments '('',)' not found. 1 pattern(s) tried: ['viewListing/(?P<listingID>[0-9]+)$']

If I pass a number instead of thisID, for instance 1, the code works fine.

So my question is, how can I make this work with a variable.

farooq

You cannot use django tags inside Javascript. They are server side. What you can do instead is cache the url in a variable and then concatenate the ID to it like this:

for(j=0; j<5; j++){
    var thisID = (data[j].id)
    var listing_url = "{% url 'viewListing' listingID=9999 %}";
    imgA.href = listing_url.replace(/9999/, thisID);
}

Credit goes to @MikeLee: Get javascript variable's value in Django url template tag

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing dictionary index as variable to url in Django template

From Dev

BUG: When passing django.messages variable through HTML tag

From Dev

Passing a Django form to a template tag

From Dev

How to use Django url template tag with variable as url path

From Dev

Passing variable in pages through URL

From Javascript

Get javascript variable's value in Django url template tag

From Dev

Passing variable to href django template

From Dev

Passing strings to Django URL in template

From Dev

Django: Add variable to template tag

From Dev

Django template variable in style tag

From Dev

Tag of a Django Template from a variable

From Dev

Passing html button tag with url href to template

From Dev

Passing object as kwargs to URL tag in template?

From Dev

is it possible to pass template variable to django template tag

From Dev

Passing variable through URL with angular js

From Dev

Passing variable to next page through url

From Dev

Passing a context variable through a class in Django

From Dev

Ruby/Sinatra: Passing a URL variable to an .erb template

From Dev

Django: Passing model object name through url

From Dev

passing parameters through url to view django

From Dev

Passing Two Parameters Through URL in Django

From Dev

Passing User attributes as a variable in Django Template

From Dev

passing variable in javascript function using django template

From Dev

Passing variable from django template to view

From Dev

Django passing view url variables inside a template

From Dev

Django - passing named parameter to url in template

From Dev

passing url parameter in django template using form

From Dev

passing video_url to html template in django

From Dev

how to use `url` template tag in `with` template tag in django templates

Related Related

  1. 1

    Passing dictionary index as variable to url in Django template

  2. 2

    BUG: When passing django.messages variable through HTML tag

  3. 3

    Passing a Django form to a template tag

  4. 4

    How to use Django url template tag with variable as url path

  5. 5

    Passing variable in pages through URL

  6. 6

    Get javascript variable's value in Django url template tag

  7. 7

    Passing variable to href django template

  8. 8

    Passing strings to Django URL in template

  9. 9

    Django: Add variable to template tag

  10. 10

    Django template variable in style tag

  11. 11

    Tag of a Django Template from a variable

  12. 12

    Passing html button tag with url href to template

  13. 13

    Passing object as kwargs to URL tag in template?

  14. 14

    is it possible to pass template variable to django template tag

  15. 15

    Passing variable through URL with angular js

  16. 16

    Passing variable to next page through url

  17. 17

    Passing a context variable through a class in Django

  18. 18

    Ruby/Sinatra: Passing a URL variable to an .erb template

  19. 19

    Django: Passing model object name through url

  20. 20

    passing parameters through url to view django

  21. 21

    Passing Two Parameters Through URL in Django

  22. 22

    Passing User attributes as a variable in Django Template

  23. 23

    passing variable in javascript function using django template

  24. 24

    Passing variable from django template to view

  25. 25

    Django passing view url variables inside a template

  26. 26

    Django - passing named parameter to url in template

  27. 27

    passing url parameter in django template using form

  28. 28

    passing video_url to html template in django

  29. 29

    how to use `url` template tag in `with` template tag in django templates

HotTag

Archive