Note that there are some explanatory texts on larger screens.

plurals
  1. POURLs and django class-based views
    primarykey
    data
    text
    <p>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.</p> <p>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!</p> <p>Calling get_absolute_url function from a shell I get:</p> <pre><code>NoReverseMatch: Reverse for 'news_detail' with arguments '('2013', 'Jan', '14', 'another-news-thang')' and keyword arguments '{}' not found. </code></pre> <p>I've read the <a href="https://docs.djangoproject.com/en/1.4/topics/class-based-views/" rel="nofollow">relevant docs</a> and the specific reference for the <a href="https://docs.djangoproject.com/en/1.4/ref/class-based-views/#datedetailview" rel="nofollow">DateDetailView</a> but I can't quite get my head round where I'm going wrong.</p> <p>My models.py is:</p> <pre><code>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 </code></pre> <p>And my urls.py is:</p> <pre><code>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&lt;year&gt;\d{4})/$', YearArchiveView.as_view( date_field = 'pub_date', model=News, context_object_name="year_archive",), ), url(r'^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\w{3})/$', MonthArchiveView.as_view( date_field = 'pub_date', model=News, context_object_name="month_archive",), ), url(r'^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\w{3})/(?P&lt;day&gt;\d{2})/$', DayArchiveView.as_view( date_field = 'pub_date', model=News, context_object_name="day_archive",), ), url(r'^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\w{3})/(?P&lt;day&gt;\d{2})/(?P&lt;slug&gt;[-\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'), ) </code></pre> <p>Sorry for the slightly messy code; I tend to clean things up once I've got them working. Any help much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload