如何自定义基于日期的通用视图的 URL?
这是我的 URL 模式:
news_info_month_dict = {
'queryset': Entry.published.filter(is_published=True),
'date_field': 'pub_date',
'month_format': '%m',
}
但是
(r'^(?P<category>[-\w]+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+).html$',
'object_detail', news_info_month_dict, 'news_detail'),
他们有一个这样的错误:
object_detail() got an unexpected keyword argument 'category'
请帮助我。谢谢!
Here is my URL pattern:
news_info_month_dict = {
'queryset': Entry.published.filter(is_published=True),
'date_field': 'pub_date',
'month_format': '%m',
}
and
(r'^(?P<category>[-\w]+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+).html
But they have an error likes this:
object_detail() got an unexpected keyword argument 'category'
Please help me. Thanks!
,
'object_detail', news_info_month_dict, 'news_detail'),
But they have an error likes this:
Please help me. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你必须编写自己的视图来代替通用的
object_detail
,像这样(未经测试)虽然可以用
object_detail
来做到这一点,但我不知道不知道 - 我很少使用通用视图。I think you'll have to write your own view in place of the generic
object_detail
, something like this (untested)Though it may be possible to do it with
object_detail
I don't know - I very rarely use generic views.在 URL 正则表达式中,
中的所有内容都会作为关键字参数传递到通用视图。问题是您使用的通用视图 (
object_detail
) 不支持所有这些参数(即category
)。有关的更多信息object_detail 通用视图及其接受的参数。
如果您需要
category
参数,只需按照 Nick 上面的建议包装视图并从 URLconf 中调用它即可。In your URL regex, everything in
<brackets>
is getting passed to the generic view as a keyword argument.The problem is that the generic view you're using (
object_detail
) doesn't support all of those arguments (namely,category
).More information about the object_detail generic view and the arguments it accepts.
If you need a
category
argument, just wrap the view as Nick suggested above and call that from your URLconf.