在博客应用程序中(我主要是在教程后构建的),我希望在帖子的单个页面视图上有下一个和上一个发布链接。该博客应用程序的urls.py文件看起来像这样:Django博客生成下一个/上一个条目
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from sandy.blog.models import Post
urlpatterns = patterns('',
url(r'^post/(?P<slug>.*)/$', list_detail.object_detail,
{'queryset': Post.objects.all(), 'template_object_name': 'post',},
name="single_post"),
url(r'^$', list_detail.object_list,
{'queryset': Post.objects.order_by('-published'), 'template_object_name': 'post',},
name="blog_home"),
)
在单一页面模板,我想这样做:
<p><a href="{% url single_post slug=[how would I derive it???] %}">Previous Post</a></p>
我有帖子对象发布在模板中,所以我可以为当前页面做类似
{%url single_post slug = post.slug%},但希望能够像slug = post [-1]那样做。金属块管他呢。有没有简单的电池包括方法来做到这一点?
当然,需要进行一些检查(看看是否有以前的帖子),我还需要重复它的'下一篇文章'链接。
我一直在绕圈走这个工作。有没有办法,还是我不得不写出完整的页面视图。
我的问题不是推导前一篇文章或下一篇文章,而只是想知道是否有某种方式可以在模板中完成,而无需为其编写视图。 – Vernon 2010-06-22 19:12:27