简体   繁体   中英

Django url template tag not working

In Django, inside a template, I do

{% url 'Customer.views.edit_customer' 1 %}

and I get a NoReverseMatch exception. However, in the line before the template render I do:

print reverse('Customer.views.edit_customer', args=(1,))

and it prints the expected URL. I know I can always just pass in the result of reverse, but I would like to understand what's going on.

The url pattern is:

url(r'^customer/edit/(\d+)$', edit_customer),

Is there a way of doing the equivalent of the reverse call in the template?

If you are using Django 1.4, remove the quotes

{% url Customer.views.edit_customer 1 %}

see https://stackoverflow.com/a/4981105/44816

Django 1.5 will accept quoted url matching patterns. You can load the url template tag from future, if you'd like to quote your patterns. Use the following before the first url tag in your templates

{% load url from future %}

The reason it didn't work the way I expected is because I forgot to put in:

{% load url from future %}

I had been using it in most of my templates, but forgot to add it to this one.

PS Thanks for the link to that question, it gave me the future answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM