Pagination Returning the wrong object (not repeating?)

I try to return paginated objects and then scroll through them. Seems pretty simple. Apparently, I missed something. Can you spot the mistake?

View:

def thumbnails(request): page = request.GET.get('page', 1) album = request.GET.get('a', None ) if (album): objects = Album_Photos.objects.filter(id=album) else: objects = None if (objects): paginator = Paginator(objects, 25) try: photos = paginator.page(page) except PageNotAnInteger: photos = paginator.page(1) except EmptyPage: photos = None #paginator.page(paginator.num_pages) return render_to_response('photos/thumbnails.html', {'photos': photos}, context_instance = RequestContext(request)) 

Template:

 {% if photos %} {% for photo in photos %} <img src="{{photo.original.url}}"> {% endfor %} {%endif%} 

Error:

 TemplateSyntaxError at /photos/thumbnails/ Caught TypeError while rendering: 'Page' object is not iterable 1 {% if photos %} 2 {% for photo in photos %} 3 <img src="{{photo.original.url}}"> 4 {% endfor %} 5 {%endif%} 
+4
source share
2 answers

Well, unlike the example in Django docs (at least in my case), you have to add .object_list to this Page object.

 {% if photos %} {% for photo in photos.object_list %} <img src="{{photo.original.url}}"> {% endfor %} {%endif%} 
+13
source

This was changed in django: somewhere between versions 1.3 and 1.6 Paginator.Page was made iterable.

If you follow the example from the current documentation using an earlier version of django, you will get this error.

Either add .object_list as Brian D. said, or update django.

0
source

All Articles