75
75
objects such as Django's ``QuerySet`` to use a more efficient ``count()``
76
76
method when available.
79
Using ``Paginator`` in a view
80
==============================
82
Here's a slightly more complex example using :class:`Paginator` in a view to
83
paginate a queryset. We give both the view and the accompanying template to
84
show how you can display the results. This example assumes you have a
85
``Contacts`` model that has already been imported.
87
The view function looks like this::
89
from django.core.paginator import Paginator, InvalidPage, EmptyPage
92
contact_list = Contacts.objects.all()
93
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
95
# Make sure page request is an int. If not, deliver first page.
97
page = int(request.GET.get('page', '1'))
101
# If page request (9999) is out of range, deliver last page of results.
103
contacts = paginator.page(page)
104
except (EmptyPage, InvalidPage):
105
contacts = paginator.page(paginator.num_pages)
107
return render_to_response('list.html', {"contacts": contacts})
109
In the template :file:`list.html`, you'll want to include navigation between
110
pages along with any interesting information from the objects themselves::
112
{% for contact in contacts.object_list %}
113
{# Each "contact" is a Contact model object. #}
114
{{ contact.full_name|upper }}<br />
118
<div class="pagination">
119
<span class="step-links">
120
{% if contacts.has_previous %}
121
<a href="?page={{ contacts.previous_page_number }}">previous</a>
124
<span class="current">
125
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
128
{% if contacts.has_next %}
129
<a href="?page={{ contacts.next_page_number }}">next</a>
78
135
``Paginator`` objects
79
136
=====================
124
181
.. attribute:: Paginator.count
126
183
The total number of objects, across all pages.
130
187
When determining the number of objects contained in ``object_list``,
131
188
``Paginator`` will first try calling ``object_list.count()``. If
133
190
fallback to using ``object_list.__len__()``. This allows objects, such
134
191
as Django's ``QuerySet``, to use a more efficient ``count()`` method
137
194
.. attribute:: Paginator.num_pages
139
196
The total number of pages.
141
198
.. attribute:: Paginator.page_range
143
200
A 1-based range of page numbers, e.g., ``[1, 2, 3, 4]``.
145
202
``InvalidPage`` exceptions
176
233
.. method:: Page.has_next()
178
235
Returns ``True`` if there's a next page.
180
237
.. method:: Page.has_previous()
182
239
Returns ``True`` if there's a previous page.
184
241
.. method:: Page.has_other_pages()
186
243
Returns ``True`` if there's a next *or* previous page.
188
245
.. method:: Page.next_page_number()
190
247
Returns the next page number. Note that this is "dumb" and will return the
191
248
next page number regardless of whether a subsequent page exists.
193
250
.. method:: Page.previous_page_number()
195
252
Returns the previous page number. Note that this is "dumb" and will return
196
253
the previous page number regardless of whether a previous page exists.
198
255
.. method:: Page.start_index()
200
257
Returns the 1-based index of the first object on the page, relative to all
201
258
of the objects in the paginator's list. For example, when paginating a list
202
259
of 5 objects with 2 objects per page, the second page's :meth:`~Page.start_index`
203
260
would return ``3``.
205
262
.. method:: Page.end_index()
207
264
Returns the 1-based index of the last object on the page, relative to all of
208
265
the objects in the paginator's list. For example, when paginating a list of
209
266
5 objects with 2 objects per page, the second page's :meth:`~Page.end_index`
215
272
.. attribute:: Page.object_list
217
274
The list of objects on this page.
219
276
.. attribute:: Page.number
221
278
The 1-based page number for this page.
223
280
.. attribute:: Page.paginator
225
282
The associated :class:`Paginator` object.