如何获取通用视图的反向 URL?
这里的问题是如何将反向用于通用视图 object_detail?
如果我像下面这样使用它,错误消息将是: NoReverseMatch 位于 /comment/add/ 未找到带有参数“()”和关键字参数“{}”的反向“”。
在views.py中:
urlresolvers.reverse('django.views.generic.list_detail.object_detail')
return HttpResponseRedirect(resp)
在urls.py中
common_info_dict = {
'extra_context':{
'blogtitle':"Thinking",
'blogsubtitle':"- blog system",
'articles_count':Entry.objects.count,
'comments_count': 0,
'visitors_count' : 0,
'category_list':Category.objects.all,
'tag_list':Tag.objects.all,
'comment_form': CommentForm,
},
}
object_detail_info_dict = {
'queryset': Entry.objects.all(),
'slug_field': 'slug',
'template_object_name': 'post',
}
object_detail_info_dict.update(common_info_dict)
urlpatterns += patterns('django.views.generic.list_detail',
(r'^posts/(?P<slug>[-\w]+)/$', 'object_detail', object_detail_info_dict),
)
Here is the question how do I use reverse for the generic view object_detail?
If I use it like the following, the error message will be:
NoReverseMatch at /comment/add/
Reverse for '' with arguments '()' and keyword arguments '{}' not found.
in views.py:
urlresolvers.reverse('django.views.generic.list_detail.object_detail')
return HttpResponseRedirect(resp)
in urls.py
common_info_dict = {
'extra_context':{
'blogtitle':"Thinking",
'blogsubtitle':"- blog system",
'articles_count':Entry.objects.count,
'comments_count': 0,
'visitors_count' : 0,
'category_list':Category.objects.all,
'tag_list':Tag.objects.all,
'comment_form': CommentForm,
},
}
object_detail_info_dict = {
'queryset': Entry.objects.all(),
'slug_field': 'slug',
'template_object_name': 'post',
}
object_detail_info_dict.update(common_info_dict)
urlpatterns += patterns('django.views.generic.list_detail',
(r'^posts/(?P<slug>[-\w]+)/
, 'object_detail', object_detail_info_dict),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在通用视图中使用反向的唯一方法 - 命名网址配置。
The only way to use reverse with generic views - named urls config.
这个问题似乎是针对旧版本的 Django 的。 我不熟悉旧的通用视图是如何工作的。 但新的基于类的通用视图也有同样的问题。
反向操作不能“开箱即用”,因为 View.as_view() 每次返回不同的包装函数,并且它们之间的比较并不相等,因此,reverse() 无法通过比较两个来找到反向路由不相等的函数。
还有另一种方法,尽管它不是标准的。 这就是我对基于类的视图所做的操作:
在本例中,我使用
plain_view
表示由as_view()
返回的不带参数的视图。 如果您将参数传递给as_view()
,那么它返回的包装器实际上将与普通包装器不同。 因此,如果您需要两者,则必须将它们分配给不同的属性:您可以链接到 urls.py 中的这些视图属性:
然后您可以通过反转视图属性来反转它们:
This question seems to be for older versions of Django. I'm not familiar with how the old generic views work. But the new class-based generic views have the same problem.
Reversing doesn't work "out of the box" because View.as_view() returns a different wrapper function each time, and they don't compare equal to each other, so reverse() can't find the reverse route by comparing two functions that aren't equal.
There is another way, although it's non-standard. This is what I do for my class-based views:
In this case, I use
plain_view
to mean the view returned byas_view()
with no arguments. If you pass arguments toas_view()
, then the wrapper it returns will actually be different to the plain one. So if you need both, you'd have to assign them to different properties:You can link to these view attributes in
urls.py
:and then you can reverse them by reversing the view attributes:
我找到了最好的解决方案,使用reverse_lazy():
https://docs .djangoproject.com/en/1.5/ref/urlresolvers/#reverse-lazy
I found the best solution, use reverse_lazy():
https://docs.djangoproject.com/en/1.5/ref/urlresolvers/#reverse-lazy