views:

137

answers:

1

I have a quite simple query set and a related generic views:

f_detail = {
         'queryset': Foto.objects.all(),
         'template_name': 'foto_dettaglio.html',
         "template_object_name" : "foto",
       }

urlpatterns = patterns('',
# This very include
(r'^foto/(?P<object_id>\d+)/$', list_detail.object_detail, f_detail, ),
)

Just a template for generating a detail page of a photo: so there's no view.


Is there an easy way to have a link to previous | next element in the template without manualy coding a view ?

Somthing like a:

{% if foto.next_item %} 
  <a href="/path/foto/{{ foto.next_item.id_field }}/">Next</a> 
{% endif}
+1  A: 
class Foto(model):
  ...
  def get_next(self):
    next = Foo.objects.filter(id__gt=self.id)
      return next[0]
    return False

  def get_prev(self):
    prev = Foo.objects.filter(id__lt=self.id)
    if prev:
      return prev[0]
    return False

you can tweak these to your liking. i just looked at your question again... to make it easier than having the if statement, you could make the methods return the markup for the link to the next/prev if there is one, otherwise return nothing. then you'd just do foto.get_next etc. also remember that querysets are lazy so you're not actually getting tons of items in next/prev.

Brandon H
Thanks, that's what I was working on right now: a custom method for my model for the next/prev links. Thanks for the example, these (id__gt|lt=self.id) look much better then whathever I was thinking of.Much appreciated.
eaman