Paginator
This class provides a mechanism for splitting a list of objects into discrete pages, supporting features like orphan management and page range elision. It includes methods for validating page numbers and retrieving specific Page objects, ensuring robust handling of out-of-range or invalid inputs. The class is iterable, allowing for easy traversal through the entire range of calculated pages.
Attributes
| Attribute | Type | Description |
|---|---|---|
| count | integer | Return the total number of objects, across all pages. |
| num_pages | integer | Return the total number of pages. |
| page_range | range | Return a 1-based range of pages for iterating through within a template for loop. |
Methods
validate_number()
@classmethod
def validate_number(
number: int|str
) - > int
Validates the provided page number against the total number of pages.
Parameters
| Name | Type | Description |
|---|---|---|
| number | `int | str` |
Returns
| Type | Description |
|---|---|
int | The validated 1-based page number as an integer |
get_page()
@classmethod
def get_page(
number: int|str
) - > [Page](page.md?sid=django_core_paginator_page)
Return a valid page, even if the page argument isn't a number or isn't in range.
Parameters
| Name | Type | Description |
|---|---|---|
| number | `int | str` |
Returns
| Type | Description |
|---|---|
[Page](page.md?sid=django_core_paginator_page) | A Page object, defaulting to the first page if the input is invalid or the last page if it is out of range |
page()
@classmethod
def page(
number: int|str
) - > [Page](page.md?sid=django_core_paginator_page)
Return a Page object for the given 1-based page number.
Parameters
| Name | Type | Description |
|---|---|---|
| number | `int | str` |
Returns
| Type | Description |
|---|---|
[Page](page.md?sid=django_core_paginator_page) | A Page object containing the slice of objects corresponding to the page number |
count()
@classmethod
def count() - > int
Return the total number of objects, across all pages.
Returns
| Type | Description |
|---|---|
int | The total count of items in the underlying object list |
num_pages()
@classmethod
def num_pages() - > int
Return the total number of pages.
Returns
| Type | Description |
|---|---|
int | The total count of pages calculated based on per_page and orphans settings |
page_range()
@classmethod
def page_range() - > range
Return a 1-based range of pages for iterating through within a template for loop.
Returns
| Type | Description |
|---|---|
range | An iterable range from 1 to the total number of pages |
get_elided_page_range()
@classmethod
def get_elided_page_range(
number: int = 1,
on_each_side: int = 3,
on_ends: int = 2
) - > generator
Generates a page range that includes ellipses to hide sections of the range when there are many pages.
Parameters
| Name | Type | Description |
|---|---|---|
| number | int = 1 | The current active page number around which the range is centered |
| on_each_side | int = 3 | The number of pages to include on each side of the current page |
| on_ends | int = 2 | The number of pages to include at the beginning and end of the range |
Returns
| Type | Description |
|---|---|
generator | A generator yielding page numbers and ellipsis strings |