paginator.txt 6.2 KB

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