Adding pagination
When you start adding content to your blog, you can easily store tens or hundreds of posts in your database. Instead of displaying all the posts on a single page, you may want to split the list of posts across several pages and include navigation links to the different pages. This functionality is called pagination, and you can find it in almost every web application that displays long lists of items.
For example, Google uses pagination to divide search results across multiple pages. Figure 2.2 shows Google’s pagination links for search result pages:
Figure 2.2: Google pagination links for search result pages
Django has a built-in pagination class that allows you to manage paginated data easily. You can define the number of objects you want to be returned per page and you can retrieve the posts that correspond to the page requested by the user.
Adding pagination to the post list view
Edit the views.py
file of the blog
application...