2008-03-19 06:36:14 +08:00
|
|
|
==========
|
|
|
|
Pagination
|
|
|
|
==========
|
|
|
|
|
2019-03-26 04:11:19 +08:00
|
|
|
Django provides high-level and low-level ways to help you manage paginated data
|
|
|
|
-- that is, data that's split across several pages, with "Previous/Next" links.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2019-03-26 04:11:19 +08:00
|
|
|
The ``Paginator`` class
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Under the hood, all methods of pagination use the
|
|
|
|
:class:`~django.core.paginator.Paginator` class. It does all the heavy lifting
|
|
|
|
of actually splitting a ``QuerySet`` into parts and handing them over to other
|
|
|
|
components.
|
2008-03-19 06:36:14 +08:00
|
|
|
|
|
|
|
Example
|
|
|
|
=======
|
|
|
|
|
2019-03-26 04:11:19 +08:00
|
|
|
Give :class:`~django.core.paginator.Paginator` a list of objects, plus the
|
|
|
|
number of items you'd like to have on each page, and it gives you methods for
|
|
|
|
accessing the items for each page::
|
2008-03-19 06:36:14 +08:00
|
|
|
|
|
|
|
>>> from django.core.paginator import Paginator
|
|
|
|
>>> objects = ['john', 'paul', 'george', 'ringo']
|
|
|
|
>>> p = Paginator(objects, 2)
|
|
|
|
|
|
|
|
>>> p.count
|
|
|
|
4
|
|
|
|
>>> p.num_pages
|
|
|
|
2
|
2017-01-19 00:51:29 +08:00
|
|
|
>>> type(p.page_range)
|
2015-06-07 03:24:02 +08:00
|
|
|
<class 'range_iterator'>
|
2008-03-19 06:36:14 +08:00
|
|
|
>>> p.page_range
|
2015-06-07 03:24:02 +08:00
|
|
|
range(1, 3)
|
2008-03-19 06:36:14 +08:00
|
|
|
|
|
|
|
>>> page1 = p.page(1)
|
|
|
|
>>> page1
|
|
|
|
<Page 1 of 2>
|
|
|
|
>>> page1.object_list
|
|
|
|
['john', 'paul']
|
|
|
|
|
|
|
|
>>> page2 = p.page(2)
|
|
|
|
>>> page2.object_list
|
|
|
|
['george', 'ringo']
|
|
|
|
>>> page2.has_next()
|
|
|
|
False
|
|
|
|
>>> page2.has_previous()
|
|
|
|
True
|
|
|
|
>>> page2.has_other_pages()
|
|
|
|
True
|
|
|
|
>>> page2.next_page_number()
|
2012-06-10 00:07:30 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
EmptyPage: That page contains no results
|
2008-03-19 06:36:14 +08:00
|
|
|
>>> page2.previous_page_number()
|
|
|
|
1
|
|
|
|
>>> page2.start_index() # The 1-based index of the first item on this page
|
|
|
|
3
|
|
|
|
>>> page2.end_index() # The 1-based index of the last item on this page
|
|
|
|
4
|
|
|
|
|
|
|
|
>>> p.page(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2008-08-28 21:44:50 +08:00
|
|
|
EmptyPage: That page number is less than 1
|
2008-03-19 06:36:14 +08:00
|
|
|
>>> p.page(3)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2008-08-28 21:44:50 +08:00
|
|
|
EmptyPage: That page contains no results
|
|
|
|
|
|
|
|
.. note::
|
2008-03-19 06:36:14 +08:00
|
|
|
|
2011-05-20 09:45:41 +08:00
|
|
|
Note that you can give ``Paginator`` a list/tuple, a Django ``QuerySet``,
|
|
|
|
or any other object with a ``count()`` or ``__len__()`` method. When
|
2008-08-24 06:25:40 +08:00
|
|
|
determining the number of objects contained in the passed object,
|
|
|
|
``Paginator`` will first try calling ``count()``, then fallback to using
|
|
|
|
``len()`` if the passed object has no ``count()`` method. This allows
|
|
|
|
objects such as Django's ``QuerySet`` to use a more efficient ``count()``
|
|
|
|
method when available.
|
|
|
|
|
2019-07-03 21:49:08 +08:00
|
|
|
Paginating a ``ListView``
|
|
|
|
=========================
|
|
|
|
|
|
|
|
:class:`django.views.generic.list.ListView` provides a builtin way to paginate
|
|
|
|
the displayed list. You can do this by adding
|
|
|
|
:attr:`~django.views.generic.list.MultipleObjectMixin.paginate_by` attribute to
|
|
|
|
your view class, for example::
|
|
|
|
|
|
|
|
from django.views.generic import ListView
|
|
|
|
|
|
|
|
from myapp.models import Contacts
|
|
|
|
|
|
|
|
class ContactsList(ListView):
|
|
|
|
paginate_by = 2
|
|
|
|
model = Contacts
|
|
|
|
|
|
|
|
The only thing your users will be missing is a way to navigate to the next or
|
|
|
|
previous page. To achieve this, add links to the next and previous page, like
|
|
|
|
shown in the below example ``list.html``.
|
|
|
|
|
2018-03-25 00:45:09 +08:00
|
|
|
.. _using-paginator-in-view:
|
2008-10-07 19:51:14 +08:00
|
|
|
|
|
|
|
Using ``Paginator`` in a view
|
2019-02-09 04:38:30 +08:00
|
|
|
=============================
|
2008-10-07 19:51:14 +08:00
|
|
|
|
2019-03-26 04:11:19 +08:00
|
|
|
Here's a slightly more complex example using
|
|
|
|
:class:`~django.core.paginator.Paginator` in a view to paginate a queryset. We
|
|
|
|
give both the view and the accompanying template to show how you can display
|
|
|
|
the results. This example assumes you have a ``Contacts`` model that has
|
|
|
|
already been imported.
|
2008-10-07 19:51:14 +08:00
|
|
|
|
|
|
|
The view function looks like this::
|
|
|
|
|
2018-12-13 01:49:47 +08:00
|
|
|
from django.core.paginator import Paginator
|
2015-12-22 23:21:24 +08:00
|
|
|
from django.shortcuts import render
|
2008-10-07 19:51:14 +08:00
|
|
|
|
|
|
|
def listing(request):
|
|
|
|
contact_list = Contacts.objects.all()
|
|
|
|
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
|
|
|
|
|
2011-04-06 17:40:26 +08:00
|
|
|
page = request.GET.get('page')
|
2017-04-06 18:01:04 +08:00
|
|
|
contacts = paginator.get_page(page)
|
2015-12-22 23:21:24 +08:00
|
|
|
return render(request, 'list.html', {'contacts': contacts})
|
2008-10-07 19:51:14 +08:00
|
|
|
|
|
|
|
In the template :file:`list.html`, you'll want to include navigation between
|
2018-03-25 00:45:09 +08:00
|
|
|
pages along with any interesting information from the objects themselves:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-10-07 19:51:14 +08:00
|
|
|
|
2011-04-06 17:40:26 +08:00
|
|
|
{% for contact in contacts %}
|
2008-10-07 19:51:14 +08:00
|
|
|
{# Each "contact" is a Contact model object. #}
|
2018-01-21 15:09:10 +08:00
|
|
|
{{ contact.full_name|upper }}<br>
|
2008-10-07 19:51:14 +08:00
|
|
|
...
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
<div class="pagination">
|
|
|
|
<span class="step-links">
|
|
|
|
{% if contacts.has_previous %}
|
2017-10-25 19:05:31 +08:00
|
|
|
<a href="?page=1">« first</a>
|
2008-10-07 19:51:14 +08:00
|
|
|
<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>
|
2017-10-25 19:05:31 +08:00
|
|
|
<a href="?page={{ contacts.paginator.num_pages }}">last »</a>
|
2008-10-07 19:51:14 +08:00
|
|
|
{% endif %}
|
|
|
|
</span>
|
|
|
|
</div>
|