Full Web Apps with FastAPI Transcripts
Chapter: View models and services
Lecture: Adding HTML for package details

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now I'm going to start again from existing HTML because, like I said, this is not a web design course, this is how do I do websites in FastAPI.
0:11 So we're gonna just look at some Chameleon and then go from there. This is what it's gonna look like, we've got our content details. Again
0:19 we're using our shared layout, as we will with basically everything. Here's our little hero section pip install such and such.
0:25 And if it's not the latest version, we're gonna do an equal equal the release version that's selected. Stuff like that.
0:31 Here's the summaries, the stuff along the side, and when you download etcetera, etcetera. So there's a lot of things that we're going to need.
0:38 For example, we need a package, which has a license, a package which has a description. We're gonna need whether or not it's the latest release.
0:47 Let's see over here. So what we're gonna do is we're just gonna go through and try to get this to run. We're gonna miss a few things
0:53 I'm sure. We'll then go back and update that as well. I'll just copy this for a second, that will give us the simplest path to do that.
1:03 Got a couple of things to import Starlette's Request, fastapi_chameleon's template.
1:10 And here let's just call this details, DetailsViewModel and let's call this details. Remember, the URL is going to be slash project,
1:21 wish it was packages and then it's some value that goes here. It was a package name. So the way we do that in FastAPI
1:28 is like this, we put it in here and we give it a type. This one is just a simple string. OK, so that means this is a required element in the URL.
1:38 There's no defaults and it's going to be a string. And because of the somewhat unusual naming structure here,
1:45 we need to set the template file explicitly. We can't just go with the standard convention. So what is it? It's packages/details.pt
1:55 Here we go. Now we don't have this DetailsViewModel. Again it's gonna have the same structure. So in the viewmodels like before,
2:02 the easiest thing to do is probably make a copy, make a new directory called packages,
2:09 and in there we're gonna make a new Python file called details_viewmodel. Go do it yourself, copy this one over. So this is gonna be
2:20 DetailsViewModel, and it's not gonna have any of this. A little placeholder. OK, so this is going to be our basic structure and
2:32 import that. We also need to pass over the package_name, right. So this is part of the URL. In order to get it, we're gonna pass it on,
2:42 so I'll copy this and put it right there. Perfect. We want to make sure they passed over a valid package name,
2:55 and that is not empty. The routing will probably do that for us. But what we can do is we actually want to make sure that not only is
3:01 this a valid name, but if we do a query against the database, we get an actual package back. What we're gonna do is gonna say
3:08 self.package is going to be package_service. And I'm gonna do a query, let's say get_package_by_id() And package_name is what we're using here.
3:22 Then, if we don't have any value for the package, we're gonna want to make sure that we return some kind of 404 and
3:29 so on. So we'll say, somewhere we'll say something like this, not that, return. OK, so we've got to write this function,
3:41 and right now this is gonna be a string.

Talk Python's Mastodon Michael Kennedy's Mastodon