Django Pagination Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in today's video i'm going to show you how to use the paginator class in django so you can create pages for your data so in this example i'm going to go from this where i have a long list of things to this where i have pages and i can click on these links and it will take me to a different page instead of displaying everything on one page so that's what i'll be covering in this video but before i get started i just want to let you know that i have a free course available called django database essentials it covers a few different things that you can do in django using the orm to interact with the database so if you're interested in that just go to prettyprinted.com django data i'll also put a link in the description below if you're interested so let's get into writing the code okay so here's what we're starting with i have a list of books and as you can see there's no pagination here i just have every single book so i have 180 books and if we look in the database we just see a list of books here and each book has both a title and an author and i just combine those two things in a template so to go to the template what we have after some basic styling is the total number of books and then we are looping over the list of books and we're just having the book title and the book author and the word by in between so i want to add some pagination to this and here's the book model so in the views here i just need to change this slightly so the first thing i need to do is i need to import the paginator so the paginator is a class and what i'll do is i'll create a pagination object from that class and that will allow me to create some pagination uh in the list so what i can do is i can import it from so from django.cor.paginator import paginator so the second paginator is a class so it needs to be capitalized and then inside of the view i can instantiate it using the list of books that i have so i'm still going to have this query where i query all the books in the database but after that i can do something like this so paginator or how about this book paginator is going to be equal to that paginator class that i just imported this query set of books here and then i can specify the number of books per page so let's say 20 books per page right so once i have that then i need to get a particular page object so this just gives me a paginator object which i can't use yet but i can get a specific page so what i want to do is i want to get the first page so i'll say page equals that book paginator dot get page and then i can pass in a page number so in this case i want one and get page won't allow you to get a page that doesn't exist if you go beyond the number of pages that actually exists it will just give you the last page so for example if there are 10 pages and you put page 100 it's still going to give you the 10th page so this is safe when you're using it because what i'm going to have later is a query string argument that allows you to change the page number and if the user changes that to something that doesn't exist you don't want the app to fail so once you have this page here then you can pass it to the context so here i can pass either page directly or i can pass the object list what i'll do is i'll pass the page so instead of books here i'll just say page and i'll pass page and we'll see how this works in the template momentarily but before i do that i want to change this count so instead of doing books.count i can still do that because it will give me the right answer but i can also get a count from the paginator so i can say book paginator dot count and it's just an attribute it's no longer a method and this will give me the number of items in the paginator which will be the same as the number of items in this query here so now if i go over to the template what i need to do is i need to modify this loop a little bit because now i'm referencing page instead of books so what i want to do is i want to say page and then i can loop over the object list so each page has an object list and this represents the objects for that particular page so since my page length here is 20 this will have 20 books in it so page.object underscore list and this will have 20 books in it so i can still say four book but four book in page dot object list right and the count should be the same so if i go over to here what i can do is refresh and we see the total number of books is still 180 but we have a much shorter list here so before what i was doing was i was showing every single book i had 180 books being displayed here now i only have 20. right so if i go back over here what i want to do is i want to then use the other methods on the page object to create the links so the reason why i passed the page object instead of page.objects list is because page has other methods that allow me to determine if i have another page available or a previous page available and it also tells me the actual page number so first let me do the page number so if i go over to the template i can add this h2 and i can say page number and then page dot number right so if i go to here we see page number one and if i change this in the view let's say i want page four and refresh this this should change to four and it does and it gives me a completely different list of books so we know that this part is working well in addition to having the actual page number what i can do is i can check to see if there exists a next page and if there exists a previous page that way i can add links so what i'll do is i'll add a div down here and this will represent the page link so i call this page links right yeah so page links and i want to have two links uh possibly so if there is a previous page i want to have a link to the previous page and if there is a next page i want to have a link to the next page so in the case where you're on the first page there won't be a previous page so nothing should appear and when you're on the last page there isn't a next page so the next link shouldn't appear so that's what i'll add here so first i need to add this if statement so if page dot has previous right so this is checking to see if there exists a previous page meaning you are on a page that's greater than the first page then i can add a link here so let me just add the uh closing tag end if and i'll add a link so before i add the actual location of the link i'll just add the link itself and i'll say a previous page and here i'll say if page has next so it works in the exact same way i'll add a link and we'll call this next page and then we just have to close out the tag there so now if i go over here and refresh we see i have a previous page in the next page they don't do anything yet because i didn't put the actual location for the links but we see they appear and no i'm on the fourth page so i have both links if i go back to the first page here and then refresh we see that the previous link disappears because there is no previous page and if i go to a page that doesn't exist so let's say page 100. like i said earlier if you go here it will give you the the last page instead of giving you an error so if i refresh this we see page 9 and we don't have a next page link so it's page 9 because i have 180 items in the database 20 items per page so 9 times 20 is 180 so there's nothing for page 10 only up to page 9. so now what i want to do is i want to allow the both the user to specify the page and i want the links to have the pages in them so what i'm going to do is i'm going to get the page number from the url so i can say page number is equal to requests.get.getpage so i'm going to have something in the url like this where you have the query string argument i can say page equals 4 for instance and that will give me page four right so i'll take this page number put it here and now let's try this so i have page four we see page four appears if i change this to eight we have page eight if i change this to one we have page one so last thing i need to do is i need to make the links work so i can do that automatically so i'll go back to the template and what i'll do is i'll go to the links and i just need to use the url tag so url and the name of my view is index and i can close that out and it's going to be the same for both and then i can add the query string argument after so the question mark and then page equals and this is going to be the same for both and then i can add the actual page number for the previous page and the page number for the next page so to get the previous page use the page object again and you say previous page number so if you're on page five this will give you page four and then i can do the same thing for the other one and instead of previous page number it will be next page number and now if i go back and refresh the page now when i click on the links they're working so page two page three four and go all the way up until i get to the last page and i can also go down through the pages until i get back to the first page and then the previous link disappears so this is pretty convenient in django if you have something that you don't want to have like a entire list of things on one page and say you want to paginate it and just note that this patchyard doesn't only work with query set lists it also works with any kind of iterable so if you have like a list of things like a regular python list you can also use a paginator and it works in exactly the same way but typically you use it for things in the database because you're not exactly sure how many items would return so that's it for using the paginator there are some other little things you can do i'll link to the documentation uh below but in this video i pretty much cover the main things so if you're interested in that you can click on the link below like i said earlier if you want to go get the django database essentials course you can go to my website prettyprinted.com django data and you can get that so that's it for this video if you have any questions feel free to leave a comment down below if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe so thank you for watching and i will talk to you next time you
Info
Channel: Pretty Printed
Views: 5,895
Rating: undefined out of 5
Keywords: pagination, django pagination, paginator, django, tutorial
Id: wmYSKVWOOTM
Channel Id: undefined
Length: 11min 30sec (690 seconds)
Published: Wed Sep 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.