Posted: 31 May 2017 | Post By: Jai Singhal |
12 min read |
You may have come across a problem of displaying many objects in a single page. Pagination allows you to divide the objects into discrete pages. It is very difficult to show up all the objects in a single page. For example, if you are running a blog website, when you want to list all the post, it is better to divide the posts into pages.
Django provides a few classes that can help you to manage your paginated data i.e., data that are split across several pages, with “Previous/Next” links or list it into discrete page numbers. You can use the Django Pagination by including the class that lives in django/core/paginator.py
Let’s understand by an example of Blog app.
Now I have created 25 posts of random title and content using a shell just to demonstrate the example.
After this, let’s create the post list view in view.py under the main Homepage URL.
Now that we have completely setup our views, it’s time create the template list.html.
Note: I have included all the necessary CSS and JS of Jquery and Bootstrap in base.html, and using the Template inheritance of Django, I have content block described in list.html
There are two types of pagination you can use in your template.
1.) Using only the previous and next buttons
Pagination can be done by providing two buttons for navigating either previous page or next page from the current page. This is the basic way to Paginate your Django Page. You could also show the page numbers along with the previous and next buttons.
2.) Using Discrete Page Numbers
Listing the range of page numbers with left and right navigation of page can be another alternative to Paginate your page.
Improving above Pagination
Suppose, you have 100’s of objects that are listed 4 per page, so it will make 20 discrete page numbers which would become cumbersome and surely look Bad.
So we can improve this Pagination by showing 5 pages numbers at a time in Pagination, and 4 other buttons that will jump to the next sequence of page numbers.
”»” and “«” buttons will help the user to jump directly to the first page and last page respectively. “…” button present at left and right will help the user to move to the first page (last page) of the previous sequence and next sequence respectively.
Note: If you want to increase/decrease the page number showing at a time(like 5 in this example), you can change it by increasing/decreasing the loop iteration shown in the below according to your need. And also you need to change the skip_pages variable in Script.
You have to include this Jquery Script to power the above HTML. Paste this script to the script container or in script of base.html
Note: If you want to change the number of pages showing at a time, change the skip_pages value according to your need keeping in mind that you have the same number of iterations discussed above.
Finally, the Pagination would look like
Play around with the design you want to show you Pagination. I have shown 3 of the most used Pagination in websites.
Comment your different ways to Paginate your Django page, it will help others too.
For more information and option available in Pagination class visit the Official Documentation of Django Pagination