summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRigel Di Scala <rigel.discala@propylon.com>2015-06-06 20:24:02 +0100
committerTim Graham <timograham@gmail.com>2015-07-03 11:34:34 -0400
commitb91a2a499fd562011fd275238924baa6002fb1f8 (patch)
treed6bd663e6a351b5b87f87223bf98b804026089a4
parentfd869cceacafdaf2c5865cc525f88357b6a26aad (diff)
Fixed #23190 -- Made Paginator.page_range an iterator
-rw-r--r--AUTHORS1
-rw-r--r--django/core/paginator.py2
-rw-r--r--docs/releases/1.9.txt13
-rw-r--r--docs/topics/pagination.txt10
-rw-r--r--tests/pagination/tests.py6
5 files changed, 29 insertions, 3 deletions
diff --git a/AUTHORS b/AUTHORS
index 0d0f454e25..05f568c1ba 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -600,6 +600,7 @@ answer newbie questions, and generally made Django that much better:
Richard Davies <richard.davies@elastichosts.com>
Richard House <Richard.House@i-logue.com>
Rick Wagner <rwagner@physics.ucsd.edu>
+ Rigel Di Scala <rigel.discala@propylon.com>
Robert Coup
Robert Myers <myer0052@gmail.com>
Roberto Aguilar <roberto@baremetal.io>
diff --git a/django/core/paginator.py b/django/core/paginator.py
index c72a6b0273..75292236fa 100644
--- a/django/core/paginator.py
+++ b/django/core/paginator.py
@@ -96,7 +96,7 @@ class Paginator(object):
Returns a 1-based range of pages for iterating through within
a template for loop.
"""
- return list(six.moves.range(1, self.num_pages + 1))
+ return six.moves.range(1, self.num_pages + 1)
page_range = property(_get_page_range)
diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt
index 0b84b3c6d3..75c126c38b 100644
--- a/docs/releases/1.9.txt
+++ b/docs/releases/1.9.txt
@@ -770,6 +770,19 @@ To fix your ``simple_tag``\s, it is best to apply the following practices:
Tags that follow these rules will be correct and safe whether they are run on
Django 1.9+ or earlier.
+``Paginator.page_range``
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+:attr:`Paginator.page_range <django.core.paginator.Paginator.page_range>` is
+now an iterator instead of a list.
+
+In versions of Django previous to 1.8, ``Paginator.page_range`` returned a
+``list`` in Python 2 and a ``range`` in Python 3. Django 1.8 consistently
+returned a list, but an iterator is more efficient.
+
+Existing code that depends on ``list`` specific features, such as indexing,
+can be ported by converting the iterator into a ``list`` using ``list()``.
+
Miscellaneous
~~~~~~~~~~~~~
diff --git a/docs/topics/pagination.txt b/docs/topics/pagination.txt
index 6ab12b2d1b..ee801ed620 100644
--- a/docs/topics/pagination.txt
+++ b/docs/topics/pagination.txt
@@ -24,8 +24,10 @@ page::
4
>>> p.num_pages
2
+ >>> type(p.page_range) # `<type 'rangeiterator'>` in Python 2.
+ <class 'range_iterator'>
>>> p.page_range
- [1, 2]
+ range(1, 3)
>>> page1 = p.page(1)
>>> page1
@@ -191,8 +193,12 @@ Attributes
.. attribute:: Paginator.page_range
- A 1-based range of page numbers, e.g., ``[1, 2, 3, 4]``.
+ A 1-based range iterator of page numbers, e.g. yielding ``[1, 2, 3, 4]``.
+ .. versionchanged:: 1.9
+
+ In older versions, ``page_range`` returned a list instead of an
+ iterator.
``InvalidPage`` exceptions
==========================
diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py
index 0757859628..31658810a2 100644
--- a/tests/pagination/tests.py
+++ b/tests/pagination/tests.py
@@ -233,6 +233,12 @@ class PaginationTests(unittest.TestCase):
self.assertEqual(page2.previous_page_number(), 1)
self.assertIsNone(page2.next_page_number())
+ def test_page_range_iterator(self):
+ """
+ Paginator.page_range should be an iterator.
+ """
+ self.assertIsInstance(Paginator([1, 2, 3], 2).page_range, type(six.moves.range(0)))
+
class ModelPaginationTests(TestCase):
"""