Full Web Apps with FastAPI Transcripts
Chapter: View models and services
Lecture: A few more view models left to create
0:00 We saw our one complex page so far, our index view, with the page that has
0:06 the counts of the packages and the latest ones and so on got converted to being, to using view models. But the others,
0:13 there's something that we could also do as well, and we'll see why. Maybe I'll write the view model here for this one,
0:18 but we're gonna leave it off for just a second. In fact, we could just use the base one on that one.
0:22 But if we look over our account view, which is right here, notice that there's a bunch of stuff going on and we're not actually using them yet
0:29 So just to, while we're on the topic, let's just go ahead and put some placeholders in place here.
0:35 So I'll take you through the process again. We're in the account view module.
0:40 So what we need to do is go over here and create a directory called account, and we have an index, we have a register, we have a login and so on.
0:50 Let's go knock those out real quick. So, IndexViewModel. I mean, we call it just AccountViewModel because it's slash account.
1:00 You can decide what you want to call it. I guess we'll go and call it that. So this'll be a class. I don't like the name.
1:06 Hold on. We'll fix that, let's go over here and rename it. What I would prefer is having account, maybe like this, it'll make it easy to read
1:19 viewmodel. OK, pass is fine. We're just gonna leave these alone for a minute. Easiest way to create more is to just copy paste.
1:29 So let's have a register_viewmodel. Make sure we change the name. And we're gonna have a login_viewmodel
1:37 as well. We don't really need a logout_viewmodel because that's just gonna make a change and send them somewhere else.
1:46 So LoginViewModel, and then we can just start using those here and say vm equals this AccountViewModel, that. It's going to need the request.
1:58 Which we want to have it at the perimeter. Thank you, like this. And then again, here what we do is just say, return the dictionary of this thing.
2:15 I'll just copy that because it's really similar, register and login. It's this view model here that you're going to see that actually is where a lot
2:32 of the magic of these view models come from. So we got that in place and then I think that's pretty much it, you'll see over
2:38 here that we could do this, just equals ViewModelBase like this and pass over the request like that.
2:49 But I'm going to leave this out for just a second. Because I wanna show you
2:54 one of the benefits that we're gonna get from using view models and sort of run into an issue here. And then we'll come back and just say,
3:00 well, look, if we add this view model, everything works great. All right, so that was just a little bit of housekeeping to
3:05 get everything set so we can start writing the rest of our application.