Django reverse url with parameters to a class based view

非 Y 不嫁゛ 提交于 2019-12-01 15:16:49

问题


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:


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 %}


来源:https://stackoverflow.com/questions/8588522/django-reverse-url-with-parameters-to-a-class-based-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!