=== modified file 'django/core/paginator.py'
|
|
|
20 | 20 | self.num_per_page = num_per_page |
21 | 21 | self.orphans = orphans |
22 | 22 | self._hits = self._pages = None |
| 23 | self._page_range = None |
23 | 24 | |
24 | 25 | def validate_page_number(self, page_number): |
25 | 26 | try: |
… |
… |
|
83 | 84 | hits = 0 |
84 | 85 | self._pages = hits // self.num_per_page + 1 |
85 | 86 | return self._pages |
| 87 | |
| 88 | def _get_page_range(self): |
| 89 | """Returns a 1-based range of pages for iterating through within a template for loop.""" |
| 90 | if self._page_range is None: |
| 91 | self._page_range = range(1, self._pages + 1) |
| 92 | return self._page_range |
86 | 93 | |
87 | 94 | hits = property(_get_hits) |
88 | 95 | pages = property(_get_pages) |
| 96 | page_range = property(_get_page_range) |
=== modified file 'django/views/generic/list_detail.py'
|
|
|
67 | 67 | 'first_on_page': paginator.first_on_page(page - 1), |
68 | 68 | 'pages': paginator.pages, |
69 | 69 | 'hits' : paginator.hits, |
| 70 | 'page_range' : paginator.page_range |
70 | 71 | }, context_processors) |
71 | 72 | else: |
72 | 73 | c = RequestContext(request, { |
=== modified file 'docs/generic_views.txt'
|
|
|
762 | 762 | * ``hits``: The total number of objects across *all* pages, not just this |
763 | 763 | page. |
764 | 764 | |
| 765 | * ``page_range``: A list of the page numbers starting at 1, ideal for ``for`` |
| 766 | loops. |
| 767 | |
765 | 768 | Notes on pagination |
766 | 769 | ~~~~~~~~~~~~~~~~~~~ |
767 | 770 | |
… |
… |
|
778 | 781 | |
779 | 782 | /objects/?page=3 |
780 | 783 | |
| 784 | * To loop over the possible page numbers use the ``page_range`` variable, |
| 785 | for example, to place a link for every page. |
| 786 | |
781 | 787 | In both cases, ``page`` is 1-based, not 0-based, so the first page would be |
782 | 788 | represented as page ``1``. |
783 | 789 | |
=== modified file 'tests/modeltests/pagination/models.py'
|
|
|
77 | 77 | >>> paginator = ObjectPaginator(Article.objects.all(), 10, orphans=1) |
78 | 78 | >>> paginator.pages |
79 | 79 | 2 |
| 80 | >>> paginator.page_range |
| 81 | [1, 2] |
80 | 82 | """} |