1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #23190 -- Made Paginator.page_range an iterator

This commit is contained in:
Rigel Di Scala 2015-06-06 20:24:02 +01:00 committed by Tim Graham
parent fd869cceac
commit b91a2a499f
5 changed files with 29 additions and 3 deletions

View File

@ -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>

View File

@ -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)

View File

@ -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
~~~~~~~~~~~~~

View File

@ -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
==========================

View File

@ -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):
"""