How to Paginate Django Query Results

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone a quick video today what I want to show you is how to use the paginate er class in django so anytime you have let's say a bunch of results from a query and you don't want to display them all on one page but and say you want to paginate them what you can do is you can use the paginate err class and django that will make this really easy to paginate so you don't have to write the code yourself because they can't get a bit tricky we try to handle all the edge cases for pagination so it's nice to have a method of doing it without having to write your own code so what I have here is I already have a view set up and I basically created some data and put it into the database it's basically just saying stuff and then a number and then I have some items and then I have a context that's empty if I say something like items is stuff items and then go to the template then I can loop over that so I can say for item in items and then we have item name and I'll just put a line break here should be BR like that now in the loop and I'll open this up and we see I just have 100 things so what I wanted to do is I want you paginate these things here so to do that if I go back to views I'm going to use the paginate class so to use it you import it first so from Django dots core dot paginate err we're going to import a class called paginate err so capital P for the class and to use it all you have to do is take your a list of items so stuff items and by the way this game uses or with a regular Python list as well but here these are the query results which is something like a list and what I can do is I can say paginate err and then stuff items and then the number of pay or the number of items per page so let's say 20 items per page and now create Apache mirror instance so P equals paginate er and then to get the results on a particular page I can say P dot page and then pass a page number so let's say I want the things on page 2 I can say page equals P dot page 2 so remember we're using the the instance of paginate er here and then calling a method call page and it's going to return everything for the second page and instead of stuff items I'll pass this to the context so now when i refresh I get stuff 20 through stuff 39 so there are 20 items here but it isn't started at 0 it starts at 20 and it goes to 39 so if I change this to say 4 it's going to give me different items so 6 see through 79 and a typical trick that people use for this is instead of hard-coding the page number is you get the page number from the query string so to do that I can say like page num equals requests dot get get and page and then I can pass this page number here and I can also have a default so if it doesn't exist it just give me the first page so then I can use it like this I can go into the URL bar and say page equals 3 and I get 4359 or I can do page 5 I get 80 through 99 and page 1 0 through 19 and if I remove it completely it still gives me page 1 so that's typically how it would be used and if we try to access a page that doesn't exist this is what happens so if I say page equals 10 so there are only a hundred items and if there are 20 items per page then I only have 5 pages so I get this error if I try to access a page number that doesn't exist and that's just an exception that you can catch so here where is the exception this empty page and I can put it around here so if I go to the part where I access the page I can say try and then accept empty page then I can say page equals P dot page one right and this empty page should come from the same place so paginate ER and now if I try this it just gives me page one if I try to access a page that does things is so I put like 50 here it gives me page 1 so if I want the total number of pages that's pretty easy to do and here I'll just print it out I take that paginate in cents and I can say so number of pages a print P dot num underscore pages and then if i refresh the page I see number of pages is 5 and if I change the count per page to let's say 12 instead of 20 and run the page again I get 0 through 11 there and I see the number of pages is 9 and then finally another typical use case is having like links for the next page in the previous page so to do that what I'll do is underneath the output two links so I'll say this one's going to be the previous page and then I'll put one next to it for the next page and so next page so I have previous page in next page and what I want to make sure happens is like I'm on page 1 right now so there is no page 0 so I don't want previous page to appear and likewise when I'm on the last page I don't want next page to appear because there is no next page so to do that all you have to do is check to see if they exists so I can take the current page so if a page dots has previous and then it will display if it has previous so let's just verify that really quick and we see it goes away and I can do the same thing for Nexo page that has next and then end if so remember this one has 12 pages so if I go to page 11 see not page 123 page 12 page 10 page 2 okay page 2 how many pages oh it has nine pages actually I have 12 per page so we have page nine here page eight and we see previous isn't appearing so let's see if page has previous so this is returning false in all cases so this is pretty easy to handle because I don't have a name page and here it's item so are actually items so items has previous and items has next so let's try that again okay so we have previous page in next page if I go to page nine we only have previous page so that's right and if I go to page one we have next page with no previous page right so that's why it's a good idea to test when it should work and when it shouldn't work because it's easy to trick yourself into thinking that you have working code so what I want to do is I want to have those actually linked to the next page so let me make sure I have a name on my URL let me go here URLs okay so name is index so if I go to my template here and then here I'll say URL the name is going to be index and then I'll just append the query string myself and I'll say items items next page number like this and it would be similar for the previous one so I'll just update this and then this should be a previous page number all right so let's see if it works and I didn't use double quotes I use single quotes around here or single curly brackets let me update that and now we see page two and the items are changing if I keep going eventually I'll get to the end and then I no longer can access next page and then previous page works and when I get back down to zero the previous page should disappear just like that so I hope I could help you understand how pagination works a little more in Django there's a little more you can do with this but this is really the basics this is probably most of what you will want to do when you're doing pagination in your app so if you have any questions about this feel free to leave a comment down below if you like this video please give me a thumbs up and if you happen to subscribe 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: 14,255
Rating: undefined out of 5
Keywords: django pagination, paginator, page results django, django
Id: 5FKL_voZuFw
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Wed Jul 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.