paginator.txt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. =========
  2. Paginator
  3. =========
  4. Django provides a few classes that help you manage paginated data -- that is,
  5. data that's split across several pages, with "Previous/Next" links. These
  6. classes live in :source:`django/core/paginator.py`.
  7. .. module:: django.core.paginator
  8. :synopsis: Classes to help you easily manage paginated data.
  9. ``Paginator`` class
  10. ===================
  11. .. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
  12. A paginator acts like a sequence of :class:`Page` when using ``len()`` or
  13. iterating it directly.
  14. .. attribute:: Paginator.object_list
  15. Required. A list, tuple, ``QuerySet``, or other sliceable object with a
  16. ``count()`` or ``__len__()`` method. For consistent pagination,
  17. ``QuerySet``\s should be ordered, e.g. with an
  18. :meth:`~django.db.models.query.QuerySet.order_by` clause or with a default
  19. :attr:`~django.db.models.Options.ordering` on the model.
  20. .. admonition:: Performance issues paginating large ``QuerySet``\s
  21. If you're using a ``QuerySet`` with a very large number of items,
  22. requesting high page numbers might be slow on some databases, because
  23. the resulting ``LIMIT``/``OFFSET`` query needs to count the number of
  24. ``OFFSET`` records which takes longer as the page number gets higher.
  25. .. attribute:: Paginator.per_page
  26. Required. The maximum number of items to include on a page, not including
  27. orphans (see the :attr:`~Paginator.orphans` optional argument below).
  28. .. attribute:: Paginator.orphans
  29. Optional. Use this when you don't want to have a last page with very few
  30. items. If the last page would normally have a number of items less than or
  31. equal to ``orphans``, then those items will be added to the previous page
  32. (which becomes the last page) instead of leaving the items on a page by
  33. themselves. For example, with 23 items, ``per_page=10``, and ``orphans=3``,
  34. there will be two pages; the first page with 10 items and the second
  35. (and last) page with 13 items. ``orphans`` defaults to zero, which means
  36. pages are never combined and the last page may have one item.
  37. .. attribute:: Paginator.allow_empty_first_page
  38. Optional. Whether or not the first page is allowed to be empty. If
  39. ``False`` and ``object_list`` is empty, then an ``EmptyPage`` error will
  40. be raised.
  41. Methods
  42. -------
  43. .. method:: Paginator.get_page(number)
  44. Returns a :class:`Page` object with the given 1-based index, while also
  45. handling out of range and invalid page numbers.
  46. If the page isn't a number, it returns the first page. If the page number
  47. is negative or greater than the number of pages, it returns the last page.
  48. Raises an :exc:`EmptyPage` exception only if you specify
  49. ``Paginator(..., allow_empty_first_page=False)`` and the ``object_list`` is
  50. empty.
  51. .. method:: Paginator.page(number)
  52. Returns a :class:`Page` object with the given 1-based index. Raises
  53. :exc:`InvalidPage` if the given page number doesn't exist.
  54. .. method:: Paginator.get_elided_page_range(number, *, on_each_side=3, on_ends=2)
  55. .. versionadded:: 3.2
  56. Returns a 1-based list of page numbers similar to
  57. :attr:`Paginator.page_range`, but may add an ellipsis to either or both
  58. sides of the current page number when :attr:`Paginator.num_pages` is large.
  59. The number of pages to include on each side of the current page number is
  60. determined by the ``on_each_side`` argument which defaults to 3.
  61. The number of pages to include at the beginning and end of page range is
  62. determined by the ``on_ends`` argument which defaults to 2.
  63. For example, with the default values for ``on_each_side`` and ``on_ends``,
  64. if the current page is 10 and there are 50 pages, the page range will be
  65. ``[1, 2, '…', 7, 8, 9, 10, 11, 12, 13, '…', 49, 50]``. This will result in
  66. pages 4, 5, and 6 to the left of and 8, 9, and 10 to the right of the
  67. current page as well as pages 1 and 2 at the start and 49 and 50 at the
  68. end.
  69. Raises :exc:`InvalidPage` if the given page number doesn't exist.
  70. Attributes
  71. ----------
  72. .. attribute:: Paginator.ELLIPSIS
  73. .. versionadded:: 3.2
  74. A translatable string used as a substitute for elided page numbers in the
  75. page range returned by :meth:`~Paginator.get_elided_page_range`. Default is
  76. ``'…'``.
  77. .. attribute:: Paginator.count
  78. The total number of objects, across all pages.
  79. .. note::
  80. When determining the number of objects contained in ``object_list``,
  81. ``Paginator`` will first try calling ``object_list.count()``. If
  82. ``object_list`` has no ``count()`` method, then ``Paginator`` will
  83. fall back to using ``len(object_list)``. This allows objects, such as
  84. ``QuerySet``, to use a more efficient ``count()`` method when
  85. available.
  86. .. attribute:: Paginator.num_pages
  87. The total number of pages.
  88. .. attribute:: Paginator.page_range
  89. A 1-based range iterator of page numbers, e.g. yielding ``[1, 2, 3, 4]``.
  90. ``Page`` class
  91. ==============
  92. You usually won't construct ``Page`` objects by hand -- you'll get them by
  93. iterating :class:`Paginator`, or by using :meth:`Paginator.page`.
  94. .. class:: Page(object_list, number, paginator)
  95. A page acts like a sequence of :attr:`Page.object_list` when using
  96. ``len()`` or iterating it directly.
  97. Methods
  98. -------
  99. .. method:: Page.has_next()
  100. Returns ``True`` if there's a next page.
  101. .. method:: Page.has_previous()
  102. Returns ``True`` if there's a previous page.
  103. .. method:: Page.has_other_pages()
  104. Returns ``True`` if there's a next **or** previous page.
  105. .. method:: Page.next_page_number()
  106. Returns the next page number. Raises :exc:`InvalidPage` if next page
  107. doesn't exist.
  108. .. method:: Page.previous_page_number()
  109. Returns the previous page number. Raises :exc:`InvalidPage` if previous
  110. page doesn't exist.
  111. .. method:: Page.start_index()
  112. Returns the 1-based index of the first object on the page, relative to all
  113. of the objects in the paginator's list. For example, when paginating a list
  114. of 5 objects with 2 objects per page, the second page's
  115. :meth:`~Page.start_index` would return ``3``.
  116. .. method:: Page.end_index()
  117. Returns the 1-based index of the last object on the page, relative to all
  118. of the objects in the paginator's list. For example, when paginating a list
  119. of 5 objects with 2 objects per page, the second page's
  120. :meth:`~Page.end_index` would return ``4``.
  121. Attributes
  122. ----------
  123. .. attribute:: Page.object_list
  124. The list of objects on this page.
  125. .. attribute:: Page.number
  126. The 1-based page number for this page.
  127. .. attribute:: Page.paginator
  128. The associated :class:`Paginator` object.
  129. Exceptions
  130. ==========
  131. .. exception:: InvalidPage
  132. A base class for exceptions raised when a paginator is passed an invalid
  133. page number.
  134. The :meth:`Paginator.page` method raises an exception if the requested page is
  135. invalid (i.e. not an integer) or contains no objects. Generally, it's enough
  136. to catch the ``InvalidPage`` exception, but if you'd like more granularity,
  137. you can catch either of the following exceptions:
  138. .. exception:: PageNotAnInteger
  139. Raised when :meth:`~Paginator.page` is given a value that isn't an integer.
  140. .. exception:: EmptyPage
  141. Raised when :meth:`~Paginator.page` is given a valid value but no objects
  142. exist on that page.
  143. Both of the exceptions are subclasses of :exc:`InvalidPage`, so you can handle
  144. them both with ``except InvalidPage``.