date_based object_detail 在 django 中给出 404
我正在尝试使用相当标准的 /
urls.py 使用包含 races.urls
(r'^races/', include('races.urls')),
我的 races/urls.py 如下:
from django.conf.urls.defaults import *
from django.views.generic import date_based
from races.models import Race
info_dict = {
'date_field': 'date',
'month_format': '%m',
'queryset': Race.objects.all,
'year': 'year',
'month': 'month',
'day': 'day',
}
urlpatterns = patterns('',
(r'^$', 'races.views.index'),
(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})$',
date_based.archive_day, dict(info_dict)),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)',
date_based.object_detail,
dict(info_dict, slug_field='slug', slug='slug',
template_name='races/race_detail.html'),
name = 'race_detail'),
)
我的 races/models.py 有
from django.db import models
from django.template.defaultfilters import slugify
class Race(models.Model):
STATUS_CHOICES = (
('Completed', 'Completed'),
('Entered', 'Entered'),
('Planned', 'Planned'),
('DNS', 'DNS'),
('DNF', 'DNF'),
)
name = models.CharField(max_length=100)
date = models.DateField('race date')
time = models.TimeField('race duration', null = True)
status = models.CharField(max_length=10, choices = STATUS_CHOICES)
slug = models.SlugField(max_length=100, editable = False)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Race, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return('race_detail', (), {
'year': self.date.year,
'month': self.date.month,
'day': self.date.day,
'slug': self.slug })
测试竞赛肯定会出现在数据库中
mysql> select * from races_race;
+----+---------------------+------------+----------+-----------+---------------------+
| id | name | date | time | status | slug |
+----+---------------------+------------+----------+-----------+---------------------+
| 1 | Race Your Pace Half | 2011-02-20 | 01:41:15 | Completed | race-your-pace-half |
+----+---------------------+------------+----------+-----------+---------------------+
但是 URL http://localhost:8000/races/2011/02/20/race- your-pace-half 不起作用。
我确信这是相当明显的事情,需要一段时间才能使上述工作发挥作用。
另一方面,永久链接也不起作用 - 即 {{race.get_absolute_url }} 在我的模板中显示为空白,我只是不知道是我的模型还是我的 URLconf 错误。
I'm trying to use permalinks in Django using a fairly standard /<app>/<year>/<month>/<day>/<slug> model. However, I get 404s with no additional information
urls.py includes races.urls using
(r'^races/', include('races.urls')),
My races/urls.py is as follows:
from django.conf.urls.defaults import *
from django.views.generic import date_based
from races.models import Race
info_dict = {
'date_field': 'date',
'month_format': '%m',
'queryset': Race.objects.all,
'year': 'year',
'month': 'month',
'day': 'day',
}
urlpatterns = patterns('',
(r'^
My races/models.py has
from django.db import models
from django.template.defaultfilters import slugify
class Race(models.Model):
STATUS_CHOICES = (
('Completed', 'Completed'),
('Entered', 'Entered'),
('Planned', 'Planned'),
('DNS', 'DNS'),
('DNF', 'DNF'),
)
name = models.CharField(max_length=100)
date = models.DateField('race date')
time = models.TimeField('race duration', null = True)
status = models.CharField(max_length=10, choices = STATUS_CHOICES)
slug = models.SlugField(max_length=100, editable = False)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Race, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return('race_detail', (), {
'year': self.date.year,
'month': self.date.month,
'day': self.date.day,
'slug': self.slug })
The test race is certainly showing up in the DB
mysql> select * from races_race;
+----+---------------------+------------+----------+-----------+---------------------+
| id | name | date | time | status | slug |
+----+---------------------+------------+----------+-----------+---------------------+
| 1 | Race Your Pace Half | 2011-02-20 | 01:41:15 | Completed | race-your-pace-half |
+----+---------------------+------------+----------+-----------+---------------------+
But the URL http://localhost:8000/races/2011/02/20/race-your-pace-half does not work.
I'm sure it's something fairly obvious, it took a while to get the above to work.
On the flip-side, the permalinks also don't work - i.e. {{ race.get_absolute_url }} comes out as blank in my templates, I just don't know if it's my model or my URLconf that is wrong.
, 'races.views.index'),
(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})
My races/models.py has
The test race is certainly showing up in the DB
But the URL http://localhost:8000/races/2011/02/20/race-your-pace-half does not work.
I'm sure it's something fairly obvious, it took a while to get the above to work.
On the flip-side, the permalinks also don't work - i.e. {{ race.get_absolute_url }} comes out as blank in my templates, I just don't know if it's my model or my URLconf that is wrong.
,
date_based.archive_day, dict(info_dict)),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)',
date_based.object_detail,
dict(info_dict, slug_field='slug', slug='slug',
template_name='races/race_detail.html'),
name = 'race_detail'),
)
My races/models.py has
The test race is certainly showing up in the DB
But the URL http://localhost:8000/races/2011/02/20/race-your-pace-half does not work.
I'm sure it's something fairly obvious, it took a while to get the above to work.
On the flip-side, the permalinks also don't work - i.e. {{ race.get_absolute_url }} comes out as blank in my templates, I just don't know if it's my model or my URLconf that is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未使用过通用视图,但我猜想无错误的 404 来自这里:
我想知道您的
info_dict
是否覆盖任何“真实”输入,因为我注意到您正在传递字符串参数对于每个必填字段 ('day': 'day'
)删除
'day'、'year' 和 'month'
作为info_dict 中的参数
因为您的 URL 已经捕获并发送这些参数。之后,我想知道您是否会收到
AttributeError
,因为Race.objects.all
是一个函数,而不是QuerySet
。让我知道结果如何!
I've never used a generic view, but an errorless 404 I'd guess comes from here:
I wonder if your
info_dict
is overriding any "real" input, because I notice you are passing in string arguments for each of your required fields ('day': 'day'
)Remove
'day', 'year', and 'month'
as your arguments ininfo_dict
because your URL is already capturing and sending those arguments.After that, I'd wonder if you'd get an
AttributeError
becauseRace.objects.all
is a function and not aQuerySet
.Let me know how it turns out!