Django template reverse url not working

I have the following in my urls.py project:

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

      

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

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

      

It returns this error:

NoReverseMatch at / watches /

The inverse for 'watch_detail' with arguments '(1,)' and keyword arguments '{}' was not found.

This confuses me because if I run "manage.py shell" I get the following results:

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

      

Any suggestions as to what might be wrong?

Thank.

+3


source to share


1 answer


What's the third parameter you have in your conf ( watch_detail

) url ? Looking at the docs your third parameter should be a dictionary.

Should your conf file read like this? -

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

      

(assumes your view is in list_detail/object_detail/watch_detail

).

To clarify, you can also pass a view function instead of a string path, so your url can be written as -



from your_app.your_module import watch_detail

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

      

If the second parameter watch_detail

is your view function.

EDIT

If watch_detail is indeed a parameter, you need to include it in the reverse of the template function -

{% url 'watch_detail', 1, watch_detail %}

      

+2


source







All Articles