Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #720 -- Added first_on_page() and last_on_page() methods to Obj…

…ectPaginator. Thanks, m@bagai.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3040 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cb9cb2045b6c92166f27239cd94f725afdcd63d7 1 parent 5077f9c
@adrianholovaty adrianholovaty authored
Showing with 29 additions and 0 deletions.
  1. +20 −0 django/core/paginator.py
  2. +9 −0 tests/modeltests/pagination/models.py
View
20 django/core/paginator.py
@@ -54,6 +54,26 @@ def has_next_page(self, page_number):
def has_previous_page(self, page_number):
return page_number > 0
+ def first_on_page(self, page_number):
+ """
+ Returns the 1-based index of the first object on the given page,
+ relative to total objects found (hits).
+ """
+ if page_number == 0:
+ return 1
+ return (self.num_per_page * page_number) + 1
+
+ def last_on_page(self, page_number):
+ """
+ Returns the 1-based index of the last object on the given page,
+ relative to total objects found (hits).
+ """
+ if page_number == 0 and self.num_per_page >= self._hits:
+ return self._hits
+ elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits:
+ return self._hits
+ return (page_number + 1) * self.num_per_page
+
def _get_hits(self):
if self._hits is None:
self._hits = self.query_set.count()
View
9 tests/modeltests/pagination/models.py
@@ -55,4 +55,13 @@ def __repr__(self):
>>> paginator.has_previous_page(1)
True
+>>> paginator.first_on_page(0)
+1
+>>> paginator.first_on_page(1)
+6
+>>> paginator.last_on_page(0)
+5
+>>> paginator.last_on_page(1)
+9
+
"""

0 comments on commit cb9cb20

Please sign in to comment.
Something went wrong with that request. Please try again.