【问题标题】:Django throws 404 at generic viewsDjango 在通用视图中抛出 404
【发布时间】:2011-02-18 13:43:00
【问题描述】:

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

这是我的更新 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

【问题讨论】:

    标签: python django http-status-code-404 django-generic-views


    【解决方案1】:

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

    (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 处显示 404。
    • 确定您现在的正则表达式正确吗?请发布您的更新版本。你有没有重启服务器?
    【解决方案2】:

    再次检查 template_name。

    【讨论】:

    • 如果模板不存在,我应该得到一个 TemplateDoesNotExist-Exception,不是吗?
    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2011-01-13
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多