Django 在通用视图中抛出 404

发布于 2024-09-01 06:13:06 字数 1459 浏览 7 评论 0原文

我正在尝试获取在 django 中工作的基于日期的存档的通用视图。 我按照教程中的描述定义了 url,但是只要我想访问其中包含变量(例如月份或年份)的 url,django 就会返回 404 错误。它甚至不会产生 TemplateDoesNotExist 异常。没有变量的普通 url 工作正常。

这是我的更新 urlconf:

from django.conf.urls.defaults import *
from zurichlive.zhl.models import Event

info_dict = {
        'queryset': Event.objects.all(),
        'date_field': 'date',
        'allow_future': 'True',
}

urlpatterns += patterns('django.views.generic.date_based',
    (r'events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', dict(info_dict, slug_field='slug', template_name='archive/detail.html')),
    (r'^events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', dict(info_dict, template_name='archive/list.html')),
    (r'^events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$','archive_day',dict(info_dict,template_name='archive/list.html')),
    (r'^events/(?P<year>\d{4})/(?P<month>[a-z]{3})/$','archive_month', dict(info_dict, template_name='archive/list.html')),
    (r'^events/(?P<year>)/$','archive_year', dict(info_dict, template_name='archive/list.html')),
    (r'^events/$','archive_index', dict(info_dict, template_name='archive/list.html')),
)

当我访问/events/2010/may/12/this-is-a-slug/时,我应该访问detail.html模板,但我得到的是404。什么我做错了吗?

我正在使用 Django 1.1.2

I'm trying to get the generic views for a date-based archive working in django.
I defined the urls as described in a tutorial, but django returns a 404 error whenever I want to access an url with a variable (such as month or year) in it. It don't even produces a TemplateDoesNotExist-execption. Normal urls without variables work fine.

Here's my updated urlconf:

from django.conf.urls.defaults import *
from zurichlive.zhl.models import Event

info_dict = {
        'queryset': Event.objects.all(),
        'date_field': 'date',
        'allow_future': 'True',
}

urlpatterns += patterns('django.views.generic.date_based',
    (r'events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

, 'object_detail', dict(info_dict, slug_field='slug', template_name='archive/detail.html')), (r'^events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

, 'object_detail', dict(info_dict, template_name='archive/list.html')), (r'^events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

,'archive_day',dict(info_dict,template_name='archive/list.html')), (r'^events/(?P<year>\d{4})/(?P<month>[a-z]{3})/

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

,'archive_month', dict(info_dict, template_name='archive/list.html')), (r'^events/(?P<year>)/

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

,'archive_year', dict(info_dict, template_name='archive/list.html')), (r'^events/

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

,'archive_index', dict(info_dict, template_name='archive/list.html')), )

When I access /events/2010/may/12/this-is-a-slug/ I should get to the detail.html template, but instead I get a 404. What am I doing wrong?

And I'm using Django 1.1.2

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

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

发布评论

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

评论(2

你丑哭了我 2024-09-08 06:13:06

您忘记了正则表达式中的反斜杠:

(r'events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/

您还(正确地)获得了以斜杠结尾的 URL 正则表达式,因此您的 URL 应该为 /events/2010/may/12/this-is-a-slug/< /代码>。

您还(正确地)获得了以斜杠结尾的 URL 正则表达式,因此您的 URL 应该为 /events/2010/may/12/this-is-a-slug/< /代码>。

You forgot the backslashes in your regexes:

(r'events/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/

Also you've (correctly) got the URL regex ending with a slash, so your URL should be /events/2010/may/12/this-is-a-slug/.

Also you've (correctly) got the URL regex ending with a slash, so your URL should be /events/2010/may/12/this-is-a-slug/.

殤城〤 2024-09-08 06:13:06

再次检查 template_name。

Check the template_name once again.

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