Django Pagination Complete Example

In this Django Pagination article i want to show you Pagination Complete Example, so when you are going to develop a blog in Django you will realize to separate the list of posts across several pages. Django has a built in pagination class to allows you to manage paginated data. 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.

 

 

Also you can check my the previous articles in django .

1: How To Build News Application In Django

2: Django Sending Email To Gmail Account 

 

 

There are two ways that you can do pagination in django, the first way is using function based view and the second way is using class based views.

 

OK now first of all you need to create a new django project using this command. i have called the project MyProject

 

 

First of all you need to change directory to the created project and after that you need to create an App, i have called the app MyApp.

 

 

Also you need to add you newly created app in your settings.py(INSTALLED_APPS).

 

 

OK now after this you need to do migrate your django project

 

 

Also you need to create a templates folder because we need to add some html files to our templates folder, make sure that after creating templates folder you need to open your settings.py and add the templates directory in to DIRS section like this.

 

 

 

1: Pagination in Function Based View

 

OK now first of all you need to open your models.py file in your app and create your model.

basically we have a model that has four fields with out id.

 

 

After that you need to do migrations like this.

 

 

First of all you need to open your views.py file and create your view function.

and you can see we have done our pagination in the Index view function

 

 

 

OK now you need to create a new urls.py in your app that you have created. and  link your view function

in there like this.

 

 

And also you need to include your app urls.py in your main project urls.py like this

 

 

 

OK now in your templates folder you need to create two html files like this.

 

So this is our index html file, also you can see at the top i have added Bootstrap CDN link because iam going to use some bootstrap styles for my pagination.  and this  code is for including of our pagination file that we have not created yet.

Since the Page object we are passing to the template is called posts, we include the pagination template in the post list template, passing the parameters to render it correctly. You can follow this method to reuse your pagination template in paginated views of different models.

Now this is our pagination.html file.

 

 

Now you can run your project and you will see the pagination.

 

 

And this will be the result for pagination.

Django Pagination Complete Example
Django Pagination Complete Example

 

 

 

 

 

2: Class Based View Pagination

 

So for class based view pagination we need to bring some changes in our views.py file like this. i have commented the first one.

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, 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.

 

 

Now after this you need to open your urls.py and comment the previous view. and add the class based view

you can see at the top we have imported the PostList, In order to keep pagination working, we have to use the right page. object that is passed to the template. Django’s ListView generic view passes the selected page in a variable called page_obj, so you have to edit your index.html template accordingly to include the paginator using the right variable, as follows

 

So if you run the project the result will be the same.

 

 

 

 

Also you can watch the complete video for this article.

 

 

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

3 thoughts on “Django Pagination Complete Example”

Comments are closed.

Codeloop
Share via
Copy link
Powered by Social Snap
×