Django 使用Coltrane博客模板的常规视图存在问题
我遵循James Bennett的django实用项目中coltrane(django博客)的例子,试图为我自己的定制博客提供一个起点。我能够完成第一章中的大部分工作,让我的博客排成一行,但当他切换到通用视图时,它似乎崩溃了 当我使用以下views.py时,我的博客工作正常:Django 使用Coltrane博客模板的常规视图存在问题,django,django-views,Django,Django Views,我遵循James Bennett的django实用项目中coltrane(django博客)的例子,试图为我自己的定制博客提供一个起点。我能够完成第一章中的大部分工作,让我的博客排成一行,但当他切换到通用视图时,它似乎崩溃了 当我使用以下views.py时,我的博客工作正常: def entry_list(request): return render_to_response('blog/entry_listing.html',
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))
URL.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),)
entry\u info\u dict={
'queryset':Entry.objects.all(),
“日期字段”:“发布日期”,
}
urlpatterns=模式(“”,
(“^blog/$”、“blog.views.entry_list”),
(“^blog/(?P\d{4})/(?P\w{3})/(?P\d{2})/(?P[-\w]+)/$”,“django.views.generic.date_-based.object_-detail”,entry_-info_-dict),)
使用这些,我可以创建博客条目列表(使用第一个url模式),然后输入“详细视图”以查看完整条目(使用第二个url模式)
然后,建议我交换我的url.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\u info\u dict={
'queryset':Entry.objects.all(),
“日期字段”:“发布日期”,
}
urlpatterns=模式(“”,
(“^blog/$”、“django.views.generic.date\u-based.archive\u-index”、条目\u-info\u-dict),
(“^blog/(?P\d{4})/(?P\w{3})/(?P\d{2})/(?P[-\w]+)/$”,“django.views.generic.date_-based.object_-detail”,entry_-info_-dict),)
我在模板中做了相应的更改(创建一个条目_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 %}
{%extends“base.html”%}
{%block content%}
这些是公开的博客条目
{对象列表%中的对象的百分比}
{%if object.status==object.LIVE_status%}
{%include“blog/entry_summary.html”%}
{%endif%}
{%endfor%}
{%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>
{{object.title}}
在{object.publish_date}上发布
{{object.summary}}
有什么想法不太正确吗?找到了答案——问题是archive\u index通用视图返回一个名为“latest”的对象和所有条目,而不是一个名为“object\u list”的对象 我使用的是django版本1.3.1