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