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:05 the counts of the packages and the latest ones and so on got converted to being,
0:10 to using view models. But the others,
0:12 there's something that we could also do as well,
0:13 and we'll see why. Maybe I'll write the view model here for this one,
0:17 but we're gonna leave it off for just a second.
0:19 In fact, we could just use the base one on that one.
0:21 But if we look over our account view, which is right here,
0:24 notice that there's a bunch of stuff going on and we're not actually using them yet
0:28 So just to, while we're on the topic,
0:31 let's just go ahead and put some placeholders in place here.
0:34 So I'll take you through the process again. We're in the account view module.
0:39 So what we need to do is go over here and create a directory called account,
0:45 and we have an index, we have a register,
0:47 we have a login and so on.
0:49 Let's go knock those out real quick.
0:50 So, IndexViewModel.
0:55 I mean, we call it just AccountViewModel because it's slash account.
0:59 You can decide what you want to call it.
1:01 I guess we'll go and call it that.
1:02 So this'll be a class. I don't like the name.
1:05 Hold on. We'll fix that, let's go over here and rename it. What I would prefer is having
1:15 account, maybe like this, it'll make it easy to read
1:18 viewmodel. OK, pass
1:23 is fine. We're just gonna leave these alone for a minute.
1:25 Easiest way to create more is to just copy paste.
1:28 So let's have a register_viewmodel.
1:31 Make sure we change the name. And we're gonna have a login_viewmodel
1:36 as well. We don't really need a logout_viewmodel because that's just
1:44 gonna make a change and send them somewhere else.
1:45 So LoginViewModel, and then we can just start using those here and say
1:50 vm equals this AccountViewModel, that. It's going to need the request.
1:57 Which we want to have it at the perimeter.
2:00 Thank you, like this. And then again,
2:10 here what we do is just say,
2:11 return the dictionary of this thing.
2:14 I'll just copy that because it's really similar, register and login.
2:25 It's this view model here that you're going to see that actually is where a lot
2:31 of the magic of these view models come from.
2:32 So we got that in place and then I think that's pretty much it, you'll see over
2:37 here that we could do this, just equals ViewModelBase
2:40 like this and pass over the request like that.
2:48 But I'm going to leave this out for just a second. Because I wanna show you
2:53 one of the benefits that we're gonna get from using view models and sort of run
2:56 into an issue here. And then we'll come back and just say,
2:59 well, look, if we add this view model,
3:00 everything works great. All right,
3:02 so that was just a little bit of housekeeping to
3:04 get everything set so we can start writing the rest of our application.