将捕获的命名正则表达式传递到通用视图中的 URL 字典

发布于 2024-08-29 23:39:24 字数 479 浏览 3 评论 0原文

我正在 Django 中使用通用视图。我想捕获 URL 中的命名组参数并将值传递给 URL 模式字典。例如,在下面的 URLConf 中,我想捕获 URL 中的 parent_slug 值并将其传递给查询集字典值,如下所示:

urlpatterns = patterns('django.views.generic.list_detail',
    (r'^(?P<parent_slug>[-\w]+)$',
     'object_list',
     {'queryset':Promotion.objects.filter(category=parent_slug)},
     'promo_promotion_list',
    ),
                      )

这可以在一个 URLConf 条目中执行吗?如果我创建一个自定义视图来捕获值并将查询集从我的覆盖视图直接传递到通用视图,会更明智吗?

I am working with a generic view in Django. I want to capture a named group parameter in the URL and pass the value to the URL pattern dictionary. For example, in the URLConf below, I want to capture the parent_slug value in the URL and pass it to the queryset dictionary value like so:

urlpatterns = patterns('django.views.generic.list_detail',
    (r'^(?P<parent_slug>[-\w]+)

Is this possible to do in one URLConf entry, or would it be wiser if I create a custom view to capture the value and pass the queryset directly to the generic view from my overridden view?

, 'object_list', {'queryset':Promotion.objects.filter(category=parent_slug)}, 'promo_promotion_list', ), )

Is this possible to do in one URLConf entry, or would it be wiser if I create a custom view to capture the value and pass the queryset directly to the generic view from my overridden view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

深海少女心 2024-09-05 23:39:24

我正在 urls.py 中进行一些重定向,如下所示,也许这也适合您?

from django.views.generic.base import RedirectView
urlpatterns = patterns('',
    (r'^manual/glossary/(?P<slug>[^/]+)/

但是,这似乎不受所有基于类的通用视图的支持:

from django.views.generic.list import ListView
urlpatterns = patterns('',
    (r'^tag/(?P<tag>\d+)/

第二个代码片段不起作用,因此您必须修补通用 ListView 或按照您的建议传递自定义视图。

, RedirectView.as_view(url='/glossary/%(slug)s/')), )

但是,这似乎不受所有基于类的通用视图的支持:


第二个代码片段不起作用,因此您必须修补通用 ListView 或按照您的建议传递自定义视图。

, ListView.as_view( queryset=Blog.Post.objects.filter(tags='%(tag)d'), paginate_by=5)), )

第二个代码片段不起作用,因此您必须修补通用 ListView 或按照您的建议传递自定义视图。

, RedirectView.as_view(url='/glossary/%(slug)s/')), )

但是,这似乎不受所有基于类的通用视图的支持:

第二个代码片段不起作用,因此您必须修补通用 ListView 或按照您的建议传递自定义视图。

I'm doing some redirects in urls.py as follows, maybe that works for you too?

from django.views.generic.base import RedirectView
urlpatterns = patterns('',
    (r'^manual/glossary/(?P<slug>[^/]+)/

However, that seems not to be supported by all class based generic views:

from django.views.generic.list import ListView
urlpatterns = patterns('',
    (r'^tag/(?P<tag>\d+)/

This second code snipplet does not work, so you would have to patch the generic ListView or pass via a custom view as you proposed.

, RedirectView.as_view(url='/glossary/%(slug)s/')), )

However, that seems not to be supported by all class based generic views:


This second code snipplet does not work, so you would have to patch the generic ListView or pass via a custom view as you proposed.

, ListView.as_view( queryset=Blog.Post.objects.filter(tags='%(tag)d'), paginate_by=5)), )

This second code snipplet does not work, so you would have to patch the generic ListView or pass via a custom view as you proposed.

, RedirectView.as_view(url='/glossary/%(slug)s/')), )

However, that seems not to be supported by all class based generic views:

This second code snipplet does not work, so you would have to patch the generic ListView or pass via a custom view as you proposed.

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