How to Perform Pagination in Django


In this article, we show how to perform pagination in Django.

With pagination, we can divide data from a database table among different pages.

Many websites perform pagination, such as forum sites. Even google, the #1 websites in the world, performs pagination. A search query make return 50,000 results, but not all 50,000 results are shown on a single page when returned to you. Instead, only 10 search results are returned per page. On the first page, there are the top 10 results. On the second page are the next 10 results (10-20). On the third page are the next 10 results (20-30).

Let's say there we have a database table of posts that users make. Let's say there are 32 posts in the database table. However, we only want to show 10 posts per page. Therefore, there will be 3 pages of 10 posts each and a final page of 2 posts.

So if you want to perform pagination of data from a database table, Django makes this relatively easy.

In this article, we show how to perform pagination of data from a database table.

We will show this in the list view of posts from the model, Post. File

The first thing we'll do is go over the file.

From the file, we retrieve the Post model from the file.

So in the file, we create a function-based view called postslist.

Within this function, we create a variable, called object_list, and set it equal to, Post.objects.all()

This gets all rows of the Post model.

We then create another variable, paginator, and set it equal to, Paginator(object_list, 5)

This line of code will show the number of objects from the database that is specified. In the above example, we specify 5, which means that the number of objects that will appear on each page is 5 (unless there are less than 5 posts for a given page). You can specify this number to be anything, but it must be an integer.

We then create a variable, page, which will get the current page that the user is on. From this data, we can know which page we are on.

We then create a try-except lbock.

We try,

If the page is not an integer, we have one exception. In this case, we return the first page.

If the page is empty, meaning it contains no post objects, then it is out of range, and we deliver the last page of results. The number of pages in pagination is stored in, paginator.num_pages. So the last page will be, paginator.num_pages

We then pass in the variables, page and posts, into the context dictionary.

We create a template file, posts_list.html, to show the posts.


Before we create the posts_list.html template file, there is one other file we need to create, pagination.html

This gives the functionality to the page navigation ability.

The code for pagination.html is shown below.

page.has_previous allows us to see whether there is a previous page. If there is, we can use the variable, {{page.previous_page_number }}, to go to the previous page.

page.paginator.num_pages gets the total number of pages that are paginated

page.has_next allows us to see whether there is a next page. If there is, we can use the variable, {{page.next_page_number }}, to go to the next page.

Template File

Now we go to the template file that holds the posts.

The contents of this file is shown below.

So now we have our template files that shows the posts paginated.

We do a for loop to retrieve all objects from the posts variable.

For this for loop, we simply output the posts title. Of course, you would add an anchor tag to each of the posts to allow for a detail view, but just for demonstration, we don't in this case.

Towards the end of the file, we include the pagination.html with the with keyword, followed by, page=posts

This gives the template file pagination capability.

Now if you go to the URL that contains the list of posts, you will see that the results are paginated.

And this is how we can perform pagination in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...