Django使用参数将url反转到基于类的视图 [英] Django reverse url with parameters to a class based view

查看:202
本文介绍了Django使用参数将url反转到基于类的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习python和django,我有一个问题。
我得到了将函数视图转换为基于类的视图的赋值。但是我的链接现在不会工作。



这些来自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%} 

但这不工作它说:

 渲染时捕获NoReverseMatch:使用参数'(20L,)'反向'rtr_contact.views.ContactView'和关键字参数'{}'未找到。 


解决方案

为了使url反转更容易,我建议你永远命名您的网址格局

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


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.

解决方案

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

这篇关于Django使用参数将url反转到基于类的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆