date_based object_detail 在 django 中给出 404

发布于 2024-10-19 09:39:07 字数 2913 浏览 1 评论 0原文

我正在尝试使用相当标准的 /////在 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 技术交流群。

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

发布评论

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

评论(1

忘羡 2024-10-26 09:39:07

我从未使用过通用视图,但我猜想无错误的 404 来自这里:

try:
    tt = time.strptime('%s-%s-%s' % (year, month, day),
                       '%s-%s-%s' % ('%Y', month_format, day_format))
    date = datetime.date(*tt[:3])
except ValueError:
    raise Http404

我想知道您的 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:

try:
    tt = time.strptime('%s-%s-%s' % (year, month, day),
                       '%s-%s-%s' % ('%Y', month_format, day_format))
    date = datetime.date(*tt[:3])
except ValueError:
    raise Http404

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 in info_dict because your URL is already capturing and sending those arguments.

After that, I'd wonder if you'd get an AttributeError because Race.objects.all is a function and not a QuerySet.

Let me know how it turns out!

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