Fixed #1795 -- Added page_range to paginators in generic list views. Thanks to polarcowz@gmail.com and Marc Fargas <telenieko@telenieko.com> for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6146 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
cb9db44938
commit
87e77ffca0
|
@ -20,6 +20,7 @@ class ObjectPaginator(object):
|
||||||
self.num_per_page = num_per_page
|
self.num_per_page = num_per_page
|
||||||
self.orphans = orphans
|
self.orphans = orphans
|
||||||
self._hits = self._pages = None
|
self._hits = self._pages = None
|
||||||
|
self._page_range = None
|
||||||
|
|
||||||
def validate_page_number(self, page_number):
|
def validate_page_number(self, page_number):
|
||||||
try:
|
try:
|
||||||
|
@ -83,6 +84,16 @@ class ObjectPaginator(object):
|
||||||
hits = 0
|
hits = 0
|
||||||
self._pages = hits // self.num_per_page + 1
|
self._pages = hits // self.num_per_page + 1
|
||||||
return self._pages
|
return self._pages
|
||||||
|
|
||||||
|
def _get_page_range(self):
|
||||||
|
"""
|
||||||
|
Returns a 1-based range of pages for iterating through within
|
||||||
|
a template for loop.
|
||||||
|
"""
|
||||||
|
if self._page_range is None:
|
||||||
|
self._page_range = range(1, self._pages + 1)
|
||||||
|
return self._page_range
|
||||||
|
|
||||||
hits = property(_get_hits)
|
hits = property(_get_hits)
|
||||||
pages = property(_get_pages)
|
pages = property(_get_pages)
|
||||||
|
page_range = property(_get_page_range)
|
||||||
|
|
|
@ -39,6 +39,8 @@ def object_list(request, queryset, paginate_by=None, page=None,
|
||||||
first_on_page
|
first_on_page
|
||||||
the result number of the first object in the
|
the result number of the first object in the
|
||||||
object_list (1-indexed)
|
object_list (1-indexed)
|
||||||
|
page_range:
|
||||||
|
A list of the page numbers (1-indexed).
|
||||||
"""
|
"""
|
||||||
if extra_context is None: extra_context = {}
|
if extra_context is None: extra_context = {}
|
||||||
queryset = queryset._clone()
|
queryset = queryset._clone()
|
||||||
|
@ -67,6 +69,7 @@ def object_list(request, queryset, paginate_by=None, page=None,
|
||||||
'first_on_page': paginator.first_on_page(page - 1),
|
'first_on_page': paginator.first_on_page(page - 1),
|
||||||
'pages': paginator.pages,
|
'pages': paginator.pages,
|
||||||
'hits' : paginator.hits,
|
'hits' : paginator.hits,
|
||||||
|
'page_range' : paginator.page_range
|
||||||
}, context_processors)
|
}, context_processors)
|
||||||
else:
|
else:
|
||||||
c = RequestContext(request, {
|
c = RequestContext(request, {
|
||||||
|
|
|
@ -765,6 +765,9 @@ If the results are paginated, the context will contain these extra variables:
|
||||||
* ``hits``: The total number of objects across *all* pages, not just this
|
* ``hits``: The total number of objects across *all* pages, not just this
|
||||||
page.
|
page.
|
||||||
|
|
||||||
|
* ``page_range``: A list of the page numbers that are available. This
|
||||||
|
is 1-based.
|
||||||
|
|
||||||
Notes on pagination
|
Notes on pagination
|
||||||
~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -781,7 +784,11 @@ specify the page number in the URL in one of two ways:
|
||||||
|
|
||||||
/objects/?page=3
|
/objects/?page=3
|
||||||
|
|
||||||
In both cases, ``page`` is 1-based, not 0-based, so the first page would be
|
* To loop over all the available page numbers, use the ``page_range``
|
||||||
|
variable. You can iterate over the list provided by ``page_range``
|
||||||
|
to create a link to every page of results.
|
||||||
|
|
||||||
|
These values and lists are is 1-based, not 0-based, so the first page would be
|
||||||
represented as page ``1``.
|
represented as page ``1``.
|
||||||
|
|
||||||
``django.views.generic.list_detail.object_detail``
|
``django.views.generic.list_detail.object_detail``
|
||||||
|
|
|
@ -77,4 +77,8 @@ True
|
||||||
>>> paginator = ObjectPaginator(Article.objects.all(), 10, orphans=1)
|
>>> paginator = ObjectPaginator(Article.objects.all(), 10, orphans=1)
|
||||||
>>> paginator.pages
|
>>> paginator.pages
|
||||||
2
|
2
|
||||||
|
|
||||||
|
# The paginator can provide a list of all available pages
|
||||||
|
>>> paginator.page_range
|
||||||
|
[1, 2]
|
||||||
"""}
|
"""}
|
||||||
|
|
Loading…
Reference in New Issue