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