pagination.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. .. _topics-pagination:
  2. ==========
  3. Pagination
  4. ==========
  5. .. module:: django.core.paginator
  6. :synopsis: Classes to help you easily manage paginated data.
  7. **New in Django development version**
  8. Django provides a few classes that help you manage paginated data -- that is,
  9. data that's split across several pages, with "Previous/Next" links. These
  10. classes live in :file:`django/core/paginator.py`.
  11. Example
  12. =======
  13. Give :class:`Paginator` a list of objects, plus the number of items you'd like to
  14. have on each page, and it gives you methods for accessing the items for each
  15. page::
  16. >>> from django.core.paginator import Paginator
  17. >>> objects = ['john', 'paul', 'george', 'ringo']
  18. >>> p = Paginator(objects, 2)
  19. >>> p.count
  20. 4
  21. >>> p.num_pages
  22. 2
  23. >>> p.page_range
  24. [1, 2]
  25. >>> page1 = p.page(1)
  26. >>> page1
  27. <Page 1 of 2>
  28. >>> page1.object_list
  29. ['john', 'paul']
  30. >>> page2 = p.page(2)
  31. >>> page2.object_list
  32. ['george', 'ringo']
  33. >>> page2.has_next()
  34. False
  35. >>> page2.has_previous()
  36. True
  37. >>> page2.has_other_pages()
  38. True
  39. >>> page2.next_page_number()
  40. 3
  41. >>> page2.previous_page_number()
  42. 1
  43. >>> page2.start_index() # The 1-based index of the first item on this page
  44. 3
  45. >>> page2.end_index() # The 1-based index of the last item on this page
  46. 4
  47. >>> p.page(0)
  48. Traceback (most recent call last):
  49. ...
  50. EmptyPage: That page number is less than 1
  51. >>> p.page(3)
  52. Traceback (most recent call last):
  53. ...
  54. EmptyPage: That page contains no results
  55. .. note::
  56. Note that you can give ``Paginator`` a list/tuple, a Django ``QuerySet``, or
  57. any other object with a ``count()`` or ``__len__()`` method. When
  58. determining the number of objects contained in the passed object,
  59. ``Paginator`` will first try calling ``count()``, then fallback to using
  60. ``len()`` if the passed object has no ``count()`` method. This allows
  61. objects such as Django's ``QuerySet`` to use a more efficient ``count()``
  62. method when available.
  63. ``Paginator`` objects
  64. =====================
  65. The :class:`Paginator` class has this constructor:
  66. .. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
  67. Required arguments
  68. ------------------
  69. ``object_list``
  70. A list, tuple, Django ``QuerySet``, or other sliceable object with a
  71. ``count()`` or ``__len__()`` method.
  72. ``per_page``
  73. The maximum number of items to include on a page, not including orphans
  74. (see the ``orphans`` optional argument below).
  75. Optional arguments
  76. ------------------
  77. ``orphans``
  78. The minimum number of items allowed on the last page, defaults to zero.
  79. Use this when you don't want to have a last page with very few items.
  80. If the last page would normally have a number of items less than or equal
  81. to ``orphans``, then those items will be added to the previous page (which
  82. becomes the last page) instead of leaving the items on a page by
  83. themselves. For example, with 23 items, ``per_page=10``, and
  84. ``orphans=3``, there will be two pages; the first page with 10 items and
  85. the second (and last) page with 13 items.
  86. ``allow_empty_first_page``
  87. Whether or not the first page is allowed to be empty. If ``False`` and
  88. ``object_list`` is empty, then an ``EmptyPage`` error will be raised.
  89. Methods
  90. -------
  91. .. method:: Paginator.page(number)
  92. Returns a :class:`Page` object with the given 1-based index. Raises
  93. :exc:`InvalidPage` if the given page number doesn't exist.
  94. Attributes
  95. ----------
  96. .. attribute:: Paginator.count
  97. The total number of objects, across all pages.
  98. .. note::
  99. When determining the number of objects contained in ``object_list``,
  100. ``Paginator`` will first try calling ``object_list.count()``. If
  101. ``object_list`` has no ``count()`` method, then ``Paginator`` will
  102. fallback to using ``object_list.__len__()``. This allows objects, such
  103. as Django's ``QuerySet``, to use a more efficient ``count()`` method
  104. when available.
  105. .. attribute:: Paginator.num_pages
  106. The total number of pages.
  107. .. attribute:: Paginator.page_range
  108. A 1-based range of page numbers, e.g., ``[1, 2, 3, 4]``.
  109. ``InvalidPage`` exceptions
  110. ==========================
  111. The ``page()`` method raises ``InvalidPage`` if the requested page is invalid
  112. (i.e., not an integer) or contains no objects. Generally, it's enough to trap
  113. the ``InvalidPage`` exception, but if you'd like more granularity, you can trap
  114. either of the following exceptions:
  115. ``PageNotAnInteger``
  116. Raised when ``page()`` is given a value that isn't an integer.
  117. ``EmptyPage``
  118. Raised when ``page()`` is given a valid value but no objects exist on that
  119. page.
  120. Both of the exceptions are subclasses of ``InvalidPage``, so you can handle
  121. them both with a simple ``except InvalidPage``.
  122. ``Page`` objects
  123. ================
  124. .. class:: Page(object_list, number, paginator):
  125. You usually won't construct :class:`Pages <Page>` by hand -- you'll get them
  126. using :meth:`Paginator.page`.
  127. Methods
  128. -------
  129. .. method:: Page.has_next()
  130. Returns ``True`` if there's a next page.
  131. .. method:: Page.has_previous()
  132. Returns ``True`` if there's a previous page.
  133. .. method:: Page.has_other_pages()
  134. Returns ``True`` if there's a next *or* previous page.
  135. .. method:: Page.next_page_number()
  136. Returns the next page number. Note that this is "dumb" and will return the
  137. next page number regardless of whether a subsequent page exists.
  138. .. method:: Page.previous_page_number()
  139. Returns the previous page number. Note that this is "dumb" and will return
  140. the previous page number regardless of whether a previous page exists.
  141. .. method:: Page.start_index()
  142. Returns the 1-based index of the first object on the page, relative to all
  143. of the objects in the paginator's list. For example, when paginating a list
  144. of 5 objects with 2 objects per page, the second page's :meth:`~Page.start_index`
  145. would return ``3``.
  146. .. method:: Page.end_index()
  147. Returns the 1-based index of the last object on the page, relative to all of
  148. the objects in the paginator's list. For example, when paginating a list of
  149. 5 objects with 2 objects per page, the second page's :meth:`~Page.end_index`
  150. would return ``4``.
  151. Attributes
  152. ----------
  153. .. attribute:: Page.object_list
  154. The list of objects on this page.
  155. .. attribute:: Page.number
  156. The 1-based page number for this page.
  157. .. attribute:: Page.paginator
  158. The associated :class:`Paginator` object.