Python Flask Tutorial: Full-Featured Web App Part 9 - Pagination

preview_player
Показать описание
In this Python Flask Tutorial, we will be learning how to use pagination within our application. Pagination allows us to only load a select number of items at a time so that our application doesn't get bogged down. We will also learn how to display links to different pages at the bottom of our page so users can quickly navigate to the page of their choice. Let's get started...

The code for this series can be found at:

The JSON for the posts I created can be found here:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python #Flask
Рекомендации по теме
Комментарии
Автор

⌨️ 1:30 Introduction to flask SQLAlchemy paginate method
⌨️ 1:45 Changing our db query in our /home route from .all( ) to .paginate( )
⌨️ 2:00 Using the command line to investigate the .paginate( ) method

⌨️ 3:08 The “Pagination” object and its attributes
— 3:15 Using the dir( ) function, i.e. dir(post)
— 3:32 Investigating the “page” object
— 3:43 Investigating the “per_page” attribute
— 3:45 posts.per_page
— 4:30 the last 5 blog posts are on second page because default is set to 20 items per page
— 4:36 How to access second page of items
— 4:50 posts = Post.query.paginate(page=2)
— 5:14 changing the number of posts per page
— 4:50 posts = #five per page

⌨️ 6:30 returning to app/routes.py to integrate pagination
— 6:55 Setting the post per page = 5
— 6:55
— 7:20 Grabbing the page we want a opposed to the default, page 1
— ^ 6:55 We obtain this from a query parameter in the url, requests library
— 6:55
— 8:05 Updating home.html template to reflect changes
— 8:52 Currently links to other pages are not wired up
— 8:56 manually pass query parameter to test other pages
— 9:08 …?page=2

⌨️ 9:34 Update template to display links/buttons to next pages
— 6:55 Setting the post per page = 5
— 10:05 Using the command line to investigate how to do this
— 10:54 Intro to the method .iter_pages( )
— 11:22 How modern webpages display the page sequence to user
— 11:50 Passing arguments to .iter_pages( ) to limit/modify amount of pages displayed to user
— 12:10 Updating home.html template to display all page links via button
— 16:44 Styling with bootstrap to show the current page the user is on

⌨️ 19:13 Sorting posts by date created
— 19:44 This done by reordering in our query
— 20:04 This will be achieved via routes.py
— 20:10 VERY SIMPLE! just add to the bd query
— 20:36 Post.query.orer_by(Post.date_posted.desc()).paginate(page=page, per_page=5)

⌨️ 21:19 Lastly, creating route for when you click on a specific user
— 21:42 Corey creates the new route
— 22:08 The new route is /user/<str:username>
— 22:48 Creating the db query within this new route (we need to filter by username now)
— 24:39 Creating a new template for a specific users page (user_posts.html)
— 27:30 We need to update the href for home.html and post.html
— 28:05 Updating the pagination links on user_posts.html

cannon
Автор

In case anyone wants to add the json file into the db, i just figure it out a way of doing it:

run python
from flaskblog import db
from flaskblog.models import Post
import json

file = open('<path-to-your-file>'. 'r')
data = json.load(file)

for x in data:
post = Post(title = x.get('title'), content = x.get('content'), user_id = x.get('user_id'))
db.session.add(post)

db.session.add(commit)

guiics
Автор

Im not yet finnished but absolutely amazing tutorials!!!! probably the best on youtube!

MikeJans
Автор

It's funny to see that part 1 has 144k views yet part 9 only 12k. I assume that some dudes have given up their flask path despite this series is so easy to follow. I myself started this tutorial late October with part 1 and hopefully will be finishing the last part by end of December. In 2019 I plan to create my own web app with the knowledge I acquired. Thanks Corey for doing such great job!

Cytzix
Автор

Utterly outstanding! I've been programming for 40 years and I've never come across tutorials as good as yours before!

iansharp
Автор

Hands Down to the best flask tutorial available on the internet still stands strong in Feb literally learned so much from these videos thanks Corey

dragon_warrior_
Автор

This series is incredibly well crafted, easy to follow with great detail. The app looks great and lots of tips for further research. Pure excellence, thank you!

andygordon
Автор

You almost never make mistakes. I do at least 2 per video in the series, and it takes be on average 15 minutes to fix the error. But I learn a lot by doing that - it makes me actually think a lot more about what I've typed.
And thanks for the tutorial. You are the best c:

StrangeIndeed
Автор

Best Flask Resource over Internet.. Thank u Corey.. You are the Best..

ritheshjarvis
Автор

I don't know if this is intentional, but the amount of content you show is just brilliant in terms of learning how to use Flask, at least for me.
Somehow you manage to make it look so easy to add these features to the code that in each of your videos by the time you get to the middle of the video and explain what the next thing you are going to do is I always get a feeling that I am going to be able to do it without help, and most of the time I succeed in it. Like making the pages look better by showing only 5 of them at once, I had no idea if it would work what I did but it was exactly the same what you did later...

Thank you for the effort you put in your work, really outstanding.

peti
Автор

What a great effort to make these amazing tutorials. I will try to get all of the points you gave. Thank you so much!

phucthaifg
Автор

This is just beautiful !! No other words came to my mind. Thanks for such a detailed explanation.

TheMayank
Автор

This tutorial series is amazing! I really appreciate it! I love when people can use own knowledge and programming skills for creating shortcuts in many boring repetitive tasks, like Corey did with the automated import of his blog articles. (look at the timestamps of post creations :D)

JirkasCZ
Автор

Great lesson! You explain it so easy and interesting! Thanks a lot!

ivanyakushchenko
Автор

Amazing Flask course, I've learned so much, Thanks a lot!

joescaos
Автор

24:19 if you don't want to use backslashes to separate long lines like this one, you can wrap the expression in parenthesis.
So instead of this:
posts = Post\
.query\
.filter_by(author=user)
You have this:
posts = (Post
.query
.filter_by(author=user))

pawekoaczynski
Автор

Exactly what I was looking for. Thank you!

irmscher
Автор

Your the best mate! Thank for all videos!!! Good job so far

untildawn
Автор

Thank you much for putting the code for this series on github! :)

hill
Автор

Thank you Mr.Schafer, your videos taught me a lot of new things

mahdinouroozi