Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Introducing the Flask framework
Lecture: Building block: Models

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here's another view method. It's going to return the popular packages. This is going to be the most popular packages on PyPI
0:08 and we're going to have the name and the summary for each one and the URL for each one, and we'll have a list of those.
0:14 And our template, popular.html, is going to need that list with all those details to generate dynamically some kind
0:21 of unordered list or whatever it is. So what we're going to do is we're going to come up with this data. Here, we're just kind of hacking it in.
0:27 But in reality, popular would probably be a query from the database with an order by number of downloads this month
0:34 or something kind of complicated like that. So we'll somehow, usually database or other API we're going to get the most popular packages
0:42 and we need to provide that to the template. So here, we say render template, give the template name and then we have keyword arguments
0:50 packages and then our list here. So in the template, it's going to be a variable to find packages, and it's going to have this value here.
0:57 And we can pass as many of these as we want or could even have a dictionary unpack it or like I said at the beginning of this chapter
1:04 there's actually some cool patterns where we would just return a dictionary itself. So this is the model that flows
1:10 from the view methods over to the template. So here, we're going to pass these by keyword arguments and we can also pass methods or functions.
1:20 That's pretty cool, because sometimes you want to have a little helper function that convert this to an integer or give me a default value
1:26 or get the name of this, unless it's not there then just return none, or something to this effect. There's some cool little helper functions
1:33 that we might want to pass along and if they're in this code or we have access to them in this code, we could actually say
1:39 helper function a equals just the name of the function and that's also available to our template. This could be data or behavior
1:47 we need to pass along to our view.

Talk Python's Mastodon Michael Kennedy's Mastodon