Django blog generate next/previous entries
In a blog application (which I have mostly built following a tutorial), I would like to have a next and previous post link on the single pa开发者_如何学Goge views of the posts. The blog app's urls.py file looks like this:
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"),
)
In the single page template I would like to do something like this:
<p><a href="{% url single_post slug=[how would I derive it???] %}">Previous Post</a></p>
I have the Post object as post available in the template, so I can do something like
{% url single_post slug=post.slug %} for the current page, but would like to be able to do something like slug=post[-1].slug or whatever. Is there a simple batteries included way to do this?
Of course there would need to be some checking (to see if there is a previous post or not), and I would need to repeat it for the 'next post' link as well.
I have been going around in circles to get this to work. Is there a way, or am I forced to write out a full view for the page.
You want the get_next_by_FOO()
and get_previous_by_FOO()
model methods.
精彩评论