I have this link in a template:
<a href="{% url show_item item.id %}">Item 1</a>
and this url in the urls.py
url(r'item/(?P<id>)/$', show_item, name="page_item")
however, this error occurs:
Reverse for 'show_item' with arguments '(63L,)' and keyword arguments '{}' not found.
I looked at this question:
how to get python to not append L to longs or ignore in django template
but it did not help.
Is there another way to use the primary key, which is an integer, in constructing URLs in templates?
Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.
Here's an example URLconf and view: # URLconf from django.urls import path from . import views urlpatterns = [ path('blog/', views.page), path('blog/page<int:num>/', views.page), ] # View (in blog/views.py) def page(request, num=1): # Output the appropriate page of blog entries, according to num. ...
Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
The URL name doesn't match. Change the template to be:
<a href="{% url page_item item.id %}">Item 1</a>
It should be page_item
not show_item
in template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With