This is the first project I’ve done using Class-based views in django (1.4) and I’m having some trouble with date-based archives not returning URLs. I’ve successfully built several apps in my project (a corporate intranet) that don’t need them, but the ‘news’ part of the site really needs a date-based archive.
The year, month and day archives all seem to work fine, but my individual articles aren’t producing the URLs I think they should. I’m pretty sure the problem is in my get_absolute_url function in models.py, because if I type the URL I want them to have directly django finds and displays the article I want!
Calling get_absolute_url function from a shell I get:
NoReverseMatch: Reverse for 'news_detail' with arguments '('2013', 'Jan', '14', 'another-news-thang')' and keyword arguments '{}' not found.
I’ve read the relevant docs and the specific reference for the DateDetailView but I can’t quite get my head round where I’m going wrong.
My models.py is:
from django.db import models
import datetime
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from phone_list.models import Person, Team
from tinymce import models as tinymce_models
from taggit.managers import TaggableManager
from easy_thumbnails.fields import ThumbnailerImageField
class LiveNewsManager(models.Manager):
def get_query_set(self):
return super(LiveNewsManager, self).get_query_set().filter(status=self.model.LIVE_STATUS)
class News(models.Model):
LIVE_STATUS=1
DRAFT_STATUS=2
HIDDEN_STATUS=3
STATUS_CHOICES=(
(LIVE_STATUS, 'Live'),
(DRAFT_STATUS, 'Draft'),
(HIDDEN_STATUS, 'Hidden'),
)
# core fields
headline=models.CharField(max_length=250)
image = ThumbnailerImageField(
upload_to='news_images',
resize_source=dict(size=(700, 500), sharpen=True),
null=True,
blank=True,
help_text = "Optional. Photographs added here are given 'feature' status and should be landscape oriented.",
)
image_caption=models.CharField(
max_length=144,
blank=True,
help_text = "DO NOT ADD IF THERE IS NO FEATURE IMAGE",
)
story = tinymce_models.HTMLField()
pub_date=models.DateTimeField(default=datetime.datetime.now)
# metadata
writer=models.ForeignKey(Person)
enable_comments=models.BooleanField(default=True)
featured=models.BooleanField(default=False)
slug=models.SlugField(unique_for_date='pub_date')
status=models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)
# categorization
tags=TaggableManager()
objects=models.Manager()
live=LiveNewsManager()
class Meta:
verbose_name_plural='News articles'
ordering=['-pub_date']
def pictures(self):
try:
return [self.image]
except:
pass
def get_absolute_url(self):
return reverse ('news_detail', args = [str(self.pub_date.strftime("%Y")), str(self.pub_date.strftime("%b")), str(self.pub_date.strftime("%d")), str(self.slug)])
def __unicode__(self):
return self.headline
And my urls.py is:
from django.conf.urls import patterns, include, url
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView
from django.views.generic import ArchiveIndexView, YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView
from datetime import date
from news.models import News
sqs = SearchQuerySet().models(News)
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view(
date_field = 'pub_date',
model=News,
context_object_name="latest_news",),
),
url(r'^(?P<year>\d{4})/$', YearArchiveView.as_view(
date_field = 'pub_date',
model=News,
context_object_name="year_archive",),
),
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/$', MonthArchiveView.as_view(
date_field = 'pub_date',
model=News,
context_object_name="month_archive",),
),
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', DayArchiveView.as_view(
date_field = 'pub_date',
model=News,
context_object_name="day_archive",),
),
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', DateDetailView.as_view(
date_field = 'pub_date',
model=News,
context_object_name="news_detail",),
),
url(r'^search/$', SearchView(
template='news/search.html',
searchqueryset=sqs,
), name='haystack_search'),
)
Sorry for the slightly messy code; I tend to clean things up once I’ve got them working. Any help much appreciated.
Nowhere here do you name your URLs.
You can’t reverse
news_detailif it hasn’t been defined somewhere.