9

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.
1
  • 1
    You should suffix your url patterns with a slash. It's a standard in Django also it makes it easier for other programs to work with it (without going into gory details ...) Commented Dec 21, 2011 at 11:21

1 Answer 1

18

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 %}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, It worked. I wish the documentation of django was easier.
I think that on the whole, Django documentation is excellent. The section on class based views is a bit bare, but hopefully it will get fleshed out in future.
It's the future, and the class-based generic-views docs are still lacking. Thanks for your answer - it helped me out too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.