Re: Reverse URL to generic view with arguments

147 views
Skip to first unread message

Daniel Roseman

unread,
Jan 19, 2013, 12:40:46 PM1/19/13
to django...@googlegroups.com
On Saturday, 19 January 2013 13:15:42 UTC, robzyb wrote:
Hi Everyone

I have the following in my project's urls.py (watch_detail is a dictionary with some parameters):

urlpatterns = patterns('',
url(r'^watches/(?P<object_id>\d+)/$', list_detail.object_detail, watch_detail, name='watch_detail'),
 )

However, the following line in a template returns an error:

<li><a href ="{% url 'watch_detail' 1 %}">A link</a></li>

The error is this:

NoReverseMatch at /watches/
Reverse for ''watch_detail'' with arguments '(1,)' and keyword arguments '{}' not found.

I really would have expected that template code to work. Because the following works in "manage.py shell":

>>> from django.core.urlresolvers import reverse
>>> reverse("watch_detail", args=(1,))
'/watches/1/'
>>>

I've done a whole lot of Google'ing, but I can't seem to figure out why it works in the shell, but doesn't work in the template.

Does anyone have any suggestions?

Thank you. 

Look closely at the error message. There are *two* single quotes around watch_detail. That's because you're (probably) using the current release, 1.4, rather than the development version, where the syntax is not to use quotes around the view name in the url tag:

    {% url watch_detail 1 %}

Note this is fully explained in both the 1.4 and dev documentation:
--
DR.

robzyb

unread,
Jan 19, 2013, 5:18:45 PM1/19/13
to django...@googlegroups.com
I do not know how I missed that. I did read the documentation, but obviously not very thoroughly.

Thank you very much for the help. I shall be more careful in the future.

Amirouche Boubekki

unread,
Jan 20, 2013, 7:00:28 AM1/20/13
to django...@googlegroups.com
Héllo Rob,


2013/1/19 robzyb <rob...@gmail.com>

I do not know how I missed that. I did read the documentation, but obviously not very thoroughly.

It's is/was a «bug» in Django, the new behavior since 1.5 is the one you used first:

One deprecated feature worth noting is the shift to “new-style” url tag. Prior to Django 1.3, syntax like {% url myview %} was interpreted incorrectly (Django considered "myview" to be a literal name of a view, not a template variable named myview). Django 1.3 and above introduced the {% load url from future %} syntax to bring in the corrected behavior where myview was seen as a variable.

The upshot of this is that if you are not using {% load url from future %} in your templates, you’ll need to change tags like{% url myview %} to {% url "myview" %}. If you were using {% load url from future %} you can simply remove that line under Django 1.5

So before 1.5 if you want to use the new (and obvious) syntax you have to include {% load url from future %} in the template. The new syntax also allows to pass a template variable to do the reversing which was not possible before.
 
Regards,


Amirouche

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Apsh1o1vYhQJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages