Skip to main content

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

AttributeTypeDescription
countintegerReturn the total number of objects, across all pages.
num_pagesintegerReturn the total number of pages.
page_rangerangeReturn 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

NameTypeDescription
number`intstr`

Returns

TypeDescription
intThe 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

NameTypeDescription
number`intstr`

Returns

TypeDescription
[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

NameTypeDescription
number`intstr`

Returns

TypeDescription
[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

TypeDescription
intThe total count of items in the underlying object list

num_pages()

@classmethod
def num_pages() - > int

Return the total number of pages.

Returns

TypeDescription
intThe 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

TypeDescription
rangeAn 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

NameTypeDescription
numberint = 1The current active page number around which the range is centered
on_each_sideint = 3The number of pages to include on each side of the current page
on_endsint = 2The number of pages to include at the beginning and end of the range

Returns

TypeDescription
generatorA generator yielding page numbers and ellipsis strings