将捕获的命名正则表达式传递到通用视图中的 URL 字典
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在 urls.py 中进行一些重定向,如下所示,也许这也适合您?
第二个代码片段不起作用,因此您必须修补通用 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?
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.