Django provides high-level and low-level ways to help you manage paginated data – that is, data that’s split across several pages, with “Previous/Next” links.

Under the hood, all methods of pagination use the Paginator class. It does all the heavy lifting of actually splitting a QuerySet into Page objects.

Note that you can give Paginator a list/tuple, a Django QuerySet , or any other object with a count() or __len__() method. When determining the number of objects contained in the passed object, Paginator will first try calling count() , then fallback to using len() if the passed object has no count() method. This allows objects such as Django’s QuerySet to use a more efficient count() method when available.

# The 1-based index of the last item on this page

# The 1-based index of the first item on this page

Give Paginator a list of objects, plus the number of items you’d like to have on each page, and it gives you methods for accessing the items for each page:

Paginating a ListView ¶

django.views.generic.list.ListView provides a builtin way to paginate the displayed list. You can do this by adding a paginate_by attribute to your view class, for example:

from django.views.generic import ListView from myapp.models import Contact class ContactList ( ListView ): paginate_by = 2 model = Contact

This limits the number of objects per page and adds a paginator and page_obj to the context . To allow your users to navigate between pages, add links to the next and previous page, in your template like this: