Module

utils.pagination

Pagination utility for splitting long lists into pages.

Classes

Paginator
Paginator for splitting a list of items into pages. Usage: paginator = Paginator(posts, per_pa…
5

Paginator for splitting a list of items into pages.

Usage:

paginator = Paginator(posts, per_page=10)
page = paginator.page(1)  # Get first page

Attributes

Name Type Description
items

List of items to paginate

per_page

Number of items per page

num_pages

Total number of pages

Methods 2

page
Get items for a specific page.
1 list[T]
def page(self, number: int) -> list[T]

Get items for a specific page.

Parameters 1
number int

Page number (1-indexed)

Returns

list[T]

List of items for that page

page_context
Get template context for a specific page.
2 dict[str, Any]
def page_context(self, page_number: int, base_url: str) -> dict[str, Any]

Get template context for a specific page.

Parameters 2
page_number int

Current page number (1-indexed)

base_url str

Base URL for pagination links (e.g., '/posts/')

Returns

dict[str, Any]

Dictionary with pagination context for templates

Internal Methods 3
__init__
Initialize the paginator.
2 None
def __init__(self, items: list[T], per_page: int = 10) -> None

Initialize the paginator.

Parameters 2
items list[T]

List of items to paginate

per_page int

Number of items per page (default: 10)

_get_page_range
Get a range of page numbers to display.
2 list[int]
def _get_page_range(self, current_page: int, window: int = 2) -> list[int]

Get a range of page numbers to display.

Parameters 2
current_page int

Current page number

window int

Number of pages to show on each side of current

Returns

list[int]

List of page numbers to display

__repr__
0 str
def __repr__(self) -> str
Returns

str