如何获取通用视图的反向 URL?

发布于 2024-07-16 19:30:10 字数 1041 浏览 4 评论 0原文

这里的问题是如何将反向用于通用视图 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┊风居住的梦幻卍 2024-07-23 19:30:10

在通用视图中使用反向的唯一方法 - 命名网址配置。

urlpatterns += patterns('django.views.generic.list_detail',
  (r'^posts/(?P<slug>[-\w]+)/
, 'object_detail',
                          object_detail_info_dict, 'post_detail'),
)

reverse('post_detail', args=('foobar',))

The only way to use reverse with generic views - named urls config.

urlpatterns += patterns('django.views.generic.list_detail',
  (r'^posts/(?P<slug>[-\w]+)/
, 'object_detail',
                          object_detail_info_dict, 'post_detail'),
)

reverse('post_detail', args=('foobar',))
妄司 2024-07-23 19:30:10

这个问题似乎是针对旧版本的 Django 的。 我不熟悉旧的通用视图是如何工作的。 但新的基于类的通用视图也有同样的问题。

反向操作不能“开箱即用”,因为 View.as_view() 每次返回不同的包装函数,并且它们之间的比较并不相等,因此,reverse() 无法通过比较两个来找到反向路由不相等的函数。

还有另一种方法,尽管它不是标准的。 这就是我对基于类的视图所做的操作:

class OrderView(LoginRequiredMixin, CreateView):
    model = Order
    form_class = OrderForm

OrderView.plain_view = staticmethod(OrderView.as_view())

在本例中,我使用 plain_view 表示由 as_view() 返回的不带参数的视图。 如果您将参数传递给 as_view(),那么它返回的包装器实际上将与普通包装器不同。 因此,如果您需要两者,则必须将它们分配给不同的属性:

OrderView.plain_view = staticmethod(OrderView.as_view())
OrderView.bonk_view = staticmethod(OrderView.as_view(whee='bonk'))

您可以链接到 urls.py 中的这些视图属性:

urlpatterns = patterns('',
    url(r'^order/

然后您可以通过反转视图属性来反转它们:

def get_success_url(self):
    return reverse(OrderView.plain_view)

def get_failure_url(self):
    return reverse(OrderView.bonk_view)
, views.OrderView.plain_view), url(r'^frob/

然后您可以通过反转视图属性来反转它们:


, views.OrderView.bonk_view),

然后您可以通过反转视图属性来反转它们:

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:

class OrderView(LoginRequiredMixin, CreateView):
    model = Order
    form_class = OrderForm

OrderView.plain_view = staticmethod(OrderView.as_view())

In this case, I use plain_view to mean the view returned by as_view() with no arguments. If you pass arguments to as_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:

OrderView.plain_view = staticmethod(OrderView.as_view())
OrderView.bonk_view = staticmethod(OrderView.as_view(whee='bonk'))

You can link to these view attributes in urls.py:

urlpatterns = patterns('',
    url(r'^order/

and then you can reverse them by reversing the view attributes:

def get_success_url(self):
    return reverse(OrderView.plain_view)

def get_failure_url(self):
    return reverse(OrderView.bonk_view)
, views.OrderView.plain_view), url(r'^frob/

and then you can reverse them by reversing the view attributes:


, views.OrderView.bonk_view),

and then you can reverse them by reversing the view attributes:

若沐 2024-07-23 19:30:10

我找到了最好的解决方案,使用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文