tags:

views:

34

answers:

1

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 page 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.

+3  A: 

You want the get_next_by_FOO() and get_previous_by_FOO() model methods.

Ignacio Vazquez-Abrams
Would I be able to use that within the template? I'm not getting it right
Vernon
You would, if you don't need to pass any arguments. If you do need to then you'll have to write a small view.
Ignacio Vazquez-Abrams