How do I display list of pages with a ListView object?

29 views
Skip to first unread message

Daniel Grace

unread,
Oct 22, 2014, 5:09:49 PM10/22/14
to django...@googlegroups.com
Hi,
How do I display list of pages with a ListView object?  For example display a link for each page (except maybe the current page):
previous 1 2 3 4 next

Currently I use the following in a view to display "previous" and "next":
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}

I tried using a for loop with a template tag and a reference to page_obj, but I could not get it to work.  Any ideas?

Thanks

Daniel Grace

unread,
Oct 22, 2014, 5:37:25 PM10/22/14
to django...@googlegroups.com
I found a solution by writing a custom template tag, passing in the current page and number of pages from page_obj.
Is that an acceptable solution or can this be done without a custom tag?

Collin Anderson

unread,
Oct 23, 2014, 8:44:21 AM10/23/14
to django...@googlegroups.com
Hi Daniel,

Would this work?
{% for p in page_obj.paginator.page_range %}
<a href="?page={{ p }}">
{% endfor %}

Collin
Reply all
Reply to author
Forward
0 new messages