2013-02-11 58 views
0

我正在关注James Bennett的Practical Django Projects中的coltrane(一个django博客)示例,以尝试为我自己的自定义博客提供一个起点。我能够在第一章中完成大部分工作,以便让我的博客全部排好队,但是当他切换到通用视图时,它似乎打破了。使用Coltrane博客模板的通用视图的问题

我的博客作品,当我使用下面的views.py:

def entry_list(request): 
    return render_to_response('blog/entry_listing.html', 
           { 'entry_list': Entry.objects.all() }, 
           context_instance=RequestContext(request)) 

def entry_detail(request, year, month, day, slug): 
    date_stamp = time.strptime(year+month+day, "%Y%b%d") 
    publish_date = datetime.date(*date_stamp[:3]) 
    entry = get_object_or_404(Entry, publish_date__year=publish_date.year, 
           publish_date__month=publish_date.month, 
           publish_date__day=publish_date.day, 
           slug=slug) 
    return render_to_response('blog/entry_detail.html', 
           { 'entry': entry }, 
           context_instance=RequestContext(request)) 

urls.py:

entry_info_dict = { 
    'queryset': Entry.objects.all(), 
    'date_field': 'publish_date', 
    } 

urlpatterns = patterns('', 
('^blog/$','blog.views.entry_list'), 
('^blog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$','django.views.generic.date_based.object_detail',entry_info_dict),) 

使用这些我可以创建博客条目的列表(使用第一URLPATTERN)然后输入“详细视图”以查看完整条目(使用第二个url模式)。

然后建议我换我的urls.py使用通用视图来显示主博客上市,所以我url.py变为:

entry_info_dict = { 
    'queryset': Entry.objects.all(), 
    'date_field': 'publish_date', 
    } 

urlpatterns = patterns('', 
('^blog/$', 'django.views.generic.date_based.archive_index', entry_info_dict), 
('^blog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$','django.views.generic.date_based.object_detail',entry_info_dict),) 

我使我的模板相应的变化(创建一个entry_archive.html,因为这个通用视图默认为_archive.html模板,并确保它使用通用的'对象'而不是'入口'作为参考),但没有任何显示。

模板是:

entry_archive.html

{% extends "base.html" %} 

{% block content %} 

<p>These are public blog entries.</p> 

<ul> 
    {% for object in object_list %} 
    {% if object.status == object.LIVE_STATUS %} 

    {% include "blog/entry_summary.html" %} 

    {% endif %} 
    {% endfor %} 
</ul> 

{% endblock %} 

entry_summary.html

<div class="blog_entry"> 
    <h2 class="blog_title">{{ object.title }}</h2> 
    <div class="blog_date">Published on {{ object.publish_date }}</div> 
    <div class="blog_summary">{{ object.summary }}</div> 
    <div class="blog_image"></div> 
    <div class="blog_url"><a href="{{ object.get_absolute_url }}">Read full entry</a></div> 
</div> 

上什么不会完全正确有什么想法?

+0

什么django版本? – 2013-02-11 19:57:01

+0

我使用的是django 1.3.1版 – 2013-02-11 20:07:11

回答

0

找到了答案 - 问题是archive_index通用视图返回一个名为'latest'的对象与所有条目,而不是一个名为'object_list'的对象!