Saturday, January 19, 2013

Re: Reverse URL to generic view with arguments

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:
https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url
--
DR.

--
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/-/tP6kpZu7Dm4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate