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%}
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%}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With