Overview
Django’s pagination system allows you to split large result sets into discrete pages. ThePaginator class handles the logic of dividing items across pages.
Paginator
The main pagination class for splitting querysets or lists into pages.The list or QuerySet to paginate. Should be ordered for consistent results.
Number of items to display per page
Minimum number of items allowed on the last page. If the last page would have fewer than this, items are added to the previous page instead.
Whether the first page is allowed to be empty
Custom error messages for ‘invalid_page’, ‘min_page’, and ‘no_results’
Properties
Total number of objects across all pages
Total number of pages
1-based range of page numbers for iteration
Methods
page()
Returns a Page object for the given page number.1-based page number to retrieve
Page object for the specified page number
PageNotAnInteger- If the page number is not an integerEmptyPage- If the page number is out of range
get_page()
Returns a valid page even if the page argument isn’t valid. Returns page 1 for invalid page numbers and the last page for out-of-range numbers.Page number to retrieve (can be invalid)
Valid Page object (never raises exceptions)
validate_number()
Validates that a page number is valid.Page number to validate
The validated page number
get_elided_page_range()
Returns a 1-based range of pages with some values elided for large page ranges.Current page number
Number of pages to show on each side of current page
Number of pages to show at the beginning and end
Generator yielding page numbers and ellipsis markers
1, 2, '…', 40, 41, 42, 43, 44, 45, 46, '…', 49, 50
Usage Example
AsyncPaginator
Async version of Paginator for use with async views and querysets.Paginator. All methods are async versions with a prefix:
await paginator.acount()await paginator.anum_pages()await paginator.apage(number)await paginator.aget_page(number)await paginator.avalidate_number(number)await paginator.aget_elided_page_range()
Page
Represents a single page of results.List of objects on this page
1-based page number
The parent Paginator instance
Properties
The list of objects on this page
The 1-based page number
The parent Paginator instance
Methods
has_next()
Returns whether there’s a next page.True if there is a next page
has_previous()
Returns whether there’s a previous page.True if there is a previous page
has_other_pages()
Returns whether there are any other pages.True if there are other pages (either previous or next)
next_page_number()
Returns the next page number.The next page number
EmptyPage if there is no next page
previous_page_number()
Returns the previous page number.The previous page number
EmptyPage if there is no previous page
start_index()
Returns the 1-based index of the first item on this page.1-based index of the first item on this page
end_index()
Returns the 1-based index of the last item on this page.1-based index of the last item on this page
Usage Example
AsyncPage
Async version of Page for use with AsyncPaginator. Supports async iteration and has async versions of methods:await page.ahas_next()await page.ahas_previous()await page.ahas_other_pages()await page.anext_page_number()await page.aprevious_page_number()await page.astart_index()await page.aend_index()await page.aget_object_list()
Exceptions
InvalidPage
Base exception class for pagination errors.PageNotAnInteger
Raised whenpage() is given a value that isn’t an integer.
EmptyPage
Raised whenpage() is given a valid value but no objects exist on that page.