我刚开始学习 python 和 django,我有一个问题。 我得到了将函数 View 转换为基于类的 View 的任务。但我的链接现在无法使用。
这些来自 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()),
这是我的链接:
{% url rtr_contact.views.ContactView contact.id %}
但这不起作用它说:
Caught NoReverseMatch while rendering: Reverse for 'rtr_contact.views.ContactView' with arguments '(20L,)' and keyword arguments '{}' not found.
最佳答案
为了使 url 反向更容易,我建议您始终 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"),
然后在模板中:
{% url contact_view contact.id %}
关于python - 带参数的 Django 反向 url 到基于类的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8588522/