summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTanner Stirrat <tstirrat@gmail.com>2019-09-20 15:04:34 -0600
committerCarlton Gibson <carlton@noumenal.es>2019-10-02 09:49:31 +0200
commit17595407ca456dc2f0cf3ddb5878ff75615c94ff (patch)
tree89de288eb3def5f0457ed2dcac57cda3451143ba
parent84322a29ce9b0940335f8ab3d60e55192bef1e50 (diff)
downloaddjango-17595407ca456dc2f0cf3ddb5878ff75615c94ff.tar.gz
Fixed #14218 -- Added Paginator.__iter__().
-rw-r--r--django/core/paginator.py4
-rw-r--r--docs/ref/paginator.txt11
-rw-r--r--docs/releases/3.1.txt5
-rw-r--r--tests/pagination/tests.py7
4 files changed, 25 insertions, 2 deletions
diff --git a/django/core/paginator.py b/django/core/paginator.py
index 1f92da5d4f..ec0ebe0403 100644
--- a/django/core/paginator.py
+++ b/django/core/paginator.py
@@ -34,6 +34,10 @@ class Paginator:
self.orphans = int(orphans)
self.allow_empty_first_page = allow_empty_first_page
+ def __iter__(self):
+ for page_number in self.page_range:
+ yield self.page(page_number)
+
def validate_number(self, number):
"""Validate the given 1-based page number."""
try:
diff --git a/docs/ref/paginator.txt b/docs/ref/paginator.txt
index 9ee6f0104c..877d6ba0d8 100644
--- a/docs/ref/paginator.txt
+++ b/docs/ref/paginator.txt
@@ -14,6 +14,13 @@ classes live in :source:`django/core/paginator.py`.
.. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
+ A paginator acts like a sequence of :class:`Page` when using ``len()`` or
+ iterating it directly.
+
+ .. versionchanged:: 3.1
+
+ Support for iterating over ``Paginator`` was added.
+
.. attribute:: Paginator.object_list
Required. A list, tuple, ``QuerySet``, or other sliceable object with a
@@ -98,8 +105,8 @@ Attributes
``Page`` class
==============
-You usually won't construct ``Page`` objects by hand -- you'll get them using
-:meth:`Paginator.page`.
+You usually won't construct ``Page`` objects by hand -- you'll get them by
+iterating :class:`Paginator`, or by using :meth:`Paginator.page`.
.. class:: Page(object_list, number, paginator)
diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt
index da954bab56..d2078469ee 100644
--- a/docs/releases/3.1.txt
+++ b/docs/releases/3.1.txt
@@ -162,6 +162,11 @@ Models
* ...
+Pagination
+~~~~~~~~~~
+
+* Support for iterating over ``Paginator`` was added.
+
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py
index ef6e355e8d..f82d333ae7 100644
--- a/tests/pagination/tests.py
+++ b/tests/pagination/tests.py
@@ -297,6 +297,13 @@ class PaginationTests(SimpleTestCase):
with self.assertRaises(EmptyPage):
paginator.get_page(1)
+ def test_paginator_iteration(self):
+ paginator = Paginator([1, 2, 3], 2)
+ page_iterator = iter(paginator)
+ for page, expected in enumerate(([1, 2], [3]), start=1):
+ with self.subTest(page=page):
+ self.assertEqual(expected, list(next(page_iterator)))
+
class ModelPaginationTests(TestCase):
"""