如何自定义基于日期的通用视图的 URL?

发布于 2024-08-14 04:14:50 字数 537 浏览 4 评论 0原文

这是我的 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 技术交流群。

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

发布评论

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

评论(2

谢绝鈎搭 2024-08-21 04:14:50

我认为你必须编写自己的视图来代替通用的 object_detail ,像这样(未经测试)

import datetime

def view_entry(request, category, year, month, day, slug):
    date = datetime.date(int(year), int(month), int(day))
    entry = get_object_or_404(Entry, slug=slug, date=date, is_published=True, category=category)
    return render_to_response('news_detail', {'object': entry})

虽然可以用 object_detail 来做到这一点,但我不知道不知道 - 我很少使用通用视图。

I think you'll have to write your own view in place of the generic object_detail, something like this (untested)

import datetime

def view_entry(request, category, year, month, day, slug):
    date = datetime.date(int(year), int(month), int(day))
    entry = get_object_or_404(Entry, slug=slug, date=date, is_published=True, category=category)
    return render_to_response('news_detail', {'object': entry})

Though it may be possible to do it with object_detail I don't know - I very rarely use generic views.

如痴如狂 2024-08-21 04:14:50

在 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.

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