Tuesday, February 28, 2012

Can't get pagination to work in django.!!!

I get this error:

Template error

In template /home/tony/Documents/vidupdate/templates/index.html, error
at line 24

Caught TypeError while rendering: 'Page' object is not iterable
14 {% else %}
15 <li><a href="/register" >Register</a></li>
16 <li><a href="/login">Login</a></li>
17 {% endif %}
18 <li><a href="/feedback">feedback</a></li>
19 <li><a href="/contact" >Contact</a></li>
20 </ul>
21 <br />
22 <h2>Entries</h2>
23 <ul>
24 {% for entry in entries %}
25 <li><a href="{{ entry.link }}" >{{ entry.title }}</a>
{{ entry.posted }} {{ entry.submitter }}</li>
26 {% endfor %}
27 </ul>
28
29 <!-- Next/Prev page links -->
30 {% if entries.object_list and posts.paginator.num_pages > 1 %}
31 <div class="pagination" style="margin-top: 20px; margin-left:
-20px; ">
32 <span class="step-links">
33 {% if entries.has_previous %}
34 <a href= "?page={{ entries.previous_page_number }}">previous</a>

this is my view:

from django.shortcuts import render_to_response
from vidupdate.posts.models import Entry
from django.template import RequestContext
from django.core.paginator import Paginator, InvalidPage, EmptyPage

def homepage(request):
entries = Entry.objects.all().order_by('-posted')
paginator = Paginator(entries, 2)

try: page = int(request.GET.get('page', '1'))
except ValueError: page = 1

try:
entries = paginator.page(page)
except (InvalidPage, EmptyPage):
entries = paginator.page(paginator.num_pages)

return render_to_response('index.html', {'entries' : entries},
context_instance=RequestContext(request))

this is in my template:

{% for entry in entries %}
<li><a href="{{ entry.link }}" >{{ entry.title }}</a>
{{ entry.posted }} {{ entry.submitter }}</li>
{% endfor %}
</ul>

<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
<a href="?
page={{ contacts.previous_page_number }}">previous</a>
{% endif %}

<span class="current">
Page {{ contacts.number }} of
{{ contacts.paginator.num_pages }}.
</span>

{% if contacts.has_next %}
<a href="?page={{ contacts.next_page_number }}">next</a>
{% endif %}
</span>
</div>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment