Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored June 01, 2006
20  django/core/paginator.py
@@ -54,6 +54,26 @@ def has_next_page(self, page_number):
54 54
     def has_previous_page(self, page_number):
55 55
         return page_number > 0
56 56
 
  57
+    def first_on_page(self, page_number):
  58
+        """
  59
+        Returns the 1-based index of the first object on the given page,
  60
+        relative to total objects found (hits).
  61
+        """
  62
+        if page_number == 0:
  63
+            return 1
  64
+        return (self.num_per_page * page_number) + 1
  65
+
  66
+    def last_on_page(self, page_number):
  67
+        """
  68
+        Returns the 1-based index of the last object on the given page,
  69
+        relative to total objects found (hits).
  70
+        """
  71
+        if page_number == 0 and self.num_per_page >= self._hits:
  72
+            return self._hits
  73
+        elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits:
  74
+            return self._hits
  75
+        return (page_number + 1) * self.num_per_page
  76
+
57 77
     def _get_hits(self):
58 78
         if self._hits is None:
59 79
             self._hits = self.query_set.count()
9  tests/modeltests/pagination/models.py
@@ -55,4 +55,13 @@ def __repr__(self):
55 55
 >>> paginator.has_previous_page(1)
56 56
 True
57 57
 
  58
+>>> paginator.first_on_page(0)
  59
+1
  60
+>>> paginator.first_on_page(1)
  61
+6
  62
+>>> paginator.last_on_page(0)
  63
+5
  64
+>>> paginator.last_on_page(1)
  65
+9
  66
+
58 67
 """

0 notes on commit cb9cb20

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