帮助基于django日期的通用视图

时间:2011-04-15 17:12:27

标签: django django-generic-views

我第一次使用基于日期的通用视图。我已经使存档页面工作,但出于某种原因,当我使用get_absolute_url方法创建指向详细信息页面的链接时,存档页面只是重新加载。 任何见解都会有所帮助,谢谢。

这是我的url.py

blog_info_dict = {
'queryset': News.objects.all(), \
'date_field': 'date_created'
}

urlpatterns = patterns('django.views.generic.date_based',
 (r'^news/$', 'archive_index', alumni_info_dict, 'blog_news_archive_index'),
 (r'^news/(?P<year>\d{4})/$', 'archive_year', \
 blog_info_dict, 'blog_news_archive_year'),
 (r'^news/(?P<year>\d{4})/(?P<month>\w{3})/$', 'archive_month', \
 blog_info_dict, 'blog_news_archive_month'),
 (r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', \
 'archive_day', blog_info_dict, 'blog_news_archive_day'),
 (r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-w]+)/$'\
  , 'object_detail', blog_info_dict,'blog_news_detail'),
 )

models.py

class News(models.Model):
      title = models.CharField('News Title', max_length=60)
      slug = models.SlugField(editable=False)
      image = models.ImageField('Image', null=True, blank=True, upload_to='uploaded_images/')
      comments = models.TextField('News Content', null=True, blank=True)
      date_created = models.DateTimeField('Date Posted', auto_now=True)
      created_by = models.ForeignKey(User)

      def save(self, *args, **kwargs):
          if not self.id:
              self.slug = slugify(self.title)
          super(News, self).save(*args, **kwargs)

      @models.permalink
      def get_absolute_url(self):
          return('blog_news_detail', (), { \
          'year': self.date_created.strftime("%Y"),
          'month': self.date_created.strftime("%b").lower(),
          'day': self.date_created.strftime("%d"),
          'slug': self.slug })

news_archive.html模板

{% extends "index.html" %}
{% block content %}
 {% for news in latest %}
 <p><a href="{{ news.get_absolute_url }}">{{ news.title }}</a> </p>
  <p>{{ news.comments }}</p>
 {% endfor %}
 {% endblock %}

news_detail.html

{% extends "news_archive.html" %}
{% block content %}
 <p>{{ object.title }}</p>
{% endblock %}

新闻对象视图

def news_view(request):
    if request.method == 'POST':
        form = NewsForm(request.POST, request.FILES)
        if form.is_valid():
            news_instance = News( \
            title = form.cleaned_data['title'],
            image = form.cleaned_data['image'],
            comments = form.cleaned_data['comments'],
            created_by = request.user)
            news_instance.save()
     else:
         form = NewsForm()
     variables = RequestContext(request, {'form': form})
        return render_to_response('news_page.html', variables)

新闻模式

class NewsForm(forms.ModelForm):
    class Meta:
        model = News
        fields = ('title', 'image', 'comments',)

1 个答案:

答案 0 :(得分:1)

刚才意识到我必须定义一个视图并传递年,月,日和slug,然后重写网址。

查看

from django.views.generic import list_detail

def news_detail_view(request, year, month, day, slug):
    response = list_detail.object_detail( \
    queryset = News.objects.all(), \
    slug = slug, slug_field = 'slug')
    return response

<强> url.py

from news.views import news_detail_view

blog_info_dict = {
'queryset': News.objects.all(), \
'date_field': 'date_created'
}



urlpatterns = patterns('django.views.generic.date_based',
     (r'^news/$', 'archive_index', alumni_info_dict, 'blog_news_archive_index'),
     (r'^news/(?P<year>\d{4})/$', 'archive_year', \
     blog_info_dict, 'blog_news_archive_year'),
     (r'^news/(?P<year>\d{4})/(?P<month>\w{3})/$', 'archive_month', \
     blog_info_dict, 'blog_news_archive_month'),
     (r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', \
     'archive_day', blog_info_dict, 'blog_news_archive_day'),
     (r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[\w-]+)/$',\
      news_detail_view, 'blog_news_detail'),
     )