2013-01-14 192 views
0

這是我在django(1.4)中使用基於類的視圖完成的第一個項目,我在使用基於日期的歸檔時沒有返回URL。我已經在我的項目(企業內部網)中成功構建了幾個不需要它們的應用程序,但該網站的'新聞'部分確實需要基於日期的歸檔。URL和django基於類的視圖

年份,月份和日期檔案似乎工作正常,但我個人的文章沒有產生我認爲他們應該的網址。我很確定問題出在models.py中的get_absolute_url函數中,因爲如果我輸入URL,我希望它們直接使用django查找並顯示我想要的文章!從殼我得到

調用get_absolute_url功能:

NoReverseMatch: Reverse for 'news_detail' with arguments '('2013', 'Jan', '14', 'another-news-thang')' and keyword arguments '{}' not found. 

我讀過relevant docs併爲DateDetailView具體可以參考,但我不能完全得到我的頭圓我要去哪裏錯了。

我的models.py是:

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 

我的urls.py是:

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

對不起,稍顯凌亂的代碼;一旦我讓他們工作,我傾向於清理它們。任何幫助非常感謝。

回答

3

你沒有在這裏命名你的網址。

如果尚未定義某處,則不能反轉news_detail

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",), 
     ), 
     name='news_detail'), #<--- you're missing this 
+0

完美,我知道這將是這樣簡單的事情。謝謝。 –