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
|
2019-12-19 05:57:36 +08:00
|
|
|
of actually splitting a ``QuerySet`` into :class:`~django.core.paginator.Page`
|
|
|
|
objects.
|
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
|
2019-12-19 05:57:36 +08:00
|
|
|
the displayed list. You can do this by adding a
|
2019-07-03 21:49:08 +08:00
|
|
|
:attr:`~django.views.generic.list.MultipleObjectMixin.paginate_by` attribute to
|
|
|
|
your view class, for example::
|
|
|
|
|
|
|
|
from django.views.generic import ListView
|
|
|
|
|
2019-12-19 05:57:36 +08:00
|
|
|
from myapp.models import Contact
|
2019-07-03 21:49:08 +08:00
|
|
|
|
2021-03-02 05:54:22 +08:00
|
|
|
class ContactListView(ListView):
|
2019-07-03 21:49:08 +08:00
|
|
|
paginate_by = 2
|
2019-12-19 05:57:36 +08:00
|
|
|
model = Contact
|
2019-07-03 21:49:08 +08:00
|
|
|
|
2019-12-19 05:57:36 +08:00
|
|
|
This limits the number of objects per page and adds a ``paginator`` and
|
|
|
|
``page_obj`` to the ``context``. To allow your users to navigate between pages,
|
|
|
|
add links to the next and previous page, in your template like this:
|
2018-03-25 00:45:09 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-10-07 19:51:14 +08:00
|
|
|
|
2019-12-19 05:57:36 +08:00
|
|
|
{% for contact in page_obj %}
|
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">
|
2019-12-19 05:57:36 +08:00
|
|
|
{% if page_obj.has_previous %}
|
2017-10-25 19:05:31 +08:00
|
|
|
<a href="?page=1">« first</a>
|
2019-12-19 05:57:36 +08:00
|
|
|
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
|
2008-10-07 19:51:14 +08:00
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
<span class="current">
|
2019-12-19 05:57:36 +08:00
|
|
|
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
|
2008-10-07 19:51:14 +08:00
|
|
|
</span>
|
|
|
|
|
2019-12-19 05:57:36 +08:00
|
|
|
{% if page_obj.has_next %}
|
|
|
|
<a href="?page={{ page_obj.next_page_number }}">next</a>
|
|
|
|
<a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
|
2008-10-07 19:51:14 +08:00
|
|
|
{% endif %}
|
|
|
|
</span>
|
|
|
|
</div>
|
2019-12-19 05:57:36 +08:00
|
|
|
|
|
|
|
.. _using-paginator-in-view:
|
|
|
|
|
|
|
|
Using ``Paginator`` in a view function
|
|
|
|
======================================
|
|
|
|
|
|
|
|
Here's an example using :class:`~django.core.paginator.Paginator` in a view
|
|
|
|
function to paginate a queryset::
|
|
|
|
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
from myapp.models import Contact
|
|
|
|
|
|
|
|
def listing(request):
|
|
|
|
contact_list = Contact.objects.all()
|
|
|
|
paginator = Paginator(contact_list, 25) # Show 25 contacts per page.
|
|
|
|
|
|
|
|
page_number = request.GET.get('page')
|
|
|
|
page_obj = paginator.get_page(page_number)
|
|
|
|
return render(request, 'list.html', {'page_obj': page_obj})
|
|
|
|
|
|
|
|
In the template :file:`list.html`, you can include navigation between pages in
|
|
|
|
the same way as in the template for the ``ListView`` above.
|