Without the paginator library, we would have to write a lot of code by hand to break our data into pages: SQL offsets, limits, calculating the number of pages, and so on. Django's paginator library does all of this grunt work for us and wraps it all into a nice object that we can pass to the template context.
Implementing the paginator library is simple and requires only a few steps:
Retrieve our
queryset
in the view using the database APIPass that
queryset
object to thepaginator
objectRetrieve a current page object from the paginator object
Pass the current page object to our template via the context
A querystring
variable maintains the state of what page we are on in the queryset so that we can navigate back and forth through the pages. The paginator takes care of handling invalid page numbers, serving 404 pages, and so on.