Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination Returning the Wrong Object (not iterable?)

I am trying to return paginated objects and then iterate through them. Seems pretty straightforward. Apparently I'm missing something, though. Can you spot the error?

The 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))

The template:

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

The 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%}
like image 480
Brian D Avatar asked Dec 12 '22 11:12

Brian D


1 Answers

Well, unlike the example in the Django docs (at least in my case), you must append .object_list to that Page object.

{% if photos %}
    {% for photo in photos.object_list %}
        <img src="{{photo.original.url}}">
    {% endfor %}
{%endif%}
like image 115
Brian D Avatar answered Dec 28 '22 20:12

Brian D