Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django reverse url with parameters to a class based view

I just started learning python and django and I have a question. I got the assignment to turn function views into class based views. But my links wont work now.

these are from urls.py:

url(r'^$', ContactIndex.as_view()),
url(r'^add$', ContactAdd.as_view()),
url(r'^([0-9]+)/update$', ContactUpdate.as_view()),
url(r'^([0-9]+)/view$', ContactView.as_view()),

This is my link :

{% url rtr_contact.views.ContactView contact.id %}

but this doesnt work it says:

Caught NoReverseMatch while rendering: Reverse for 'rtr_contact.views.ContactView' with arguments '(20L,)' and keyword arguments '{}' not found.
like image 346
user769498 Avatar asked Dec 21 '11 10:12

user769498


1 Answers

To make url reversing easy, I recommend that you always name your url patterns.

url(r'^$', ContactIndex.as_view(), name="contact_index"),
url(r'^add$', ContactAdd.as_view(), name="contact_add"),
url(r'^([0-9]+)/update$', ContactUpdate.as_view(), name="contact_update"),
url(r'^([0-9]+)/view$', ContactView.as_view(), name="contact_view"),

Then in the template:

{% url contact_view contact.id %}
like image 81
Alasdair Avatar answered Sep 28 '22 01:09

Alasdair