Django 在通用视图中抛出 404
我正在尝试获取在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忘记了正则表达式中的反斜杠:
您还(正确地)获得了以斜杠结尾的 URL 正则表达式,因此您的 URL 应该为
/events/2010/may/12/this-is-a-slug/< /代码>。
You forgot the backslashes in your regexes:
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/
.再次检查 template_name。
Check the template_name once again.