diff --git a/django/core/paginator.py b/django/core/paginator.py index 6e01c1ccec..f4941cb678 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -54,6 +54,26 @@ class ObjectPaginator: 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() diff --git a/tests/modeltests/pagination/models.py b/tests/modeltests/pagination/models.py index fa45616b52..3667c84d69 100644 --- a/tests/modeltests/pagination/models.py +++ b/tests/modeltests/pagination/models.py @@ -55,4 +55,13 @@ False >>> 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 + """