开发者

date_based object_detail giving 404 in django

开发者 https://www.devze.com 2023-02-13 11:54 出处:网络
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

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'^$', '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'),
)

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.y开发者_StackOverflow社区ear,
            '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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号