Full Web Apps with FastAPI Transcripts
Chapter: Users and HTML forms
Lecture: GET/POST/Redirect for registration

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So far, our login and register haven't done anything.
0:03 Remember, if we look over in the views,
0:05 they just return empty stuff and they were actually crashing because they didn't know a template that
0:10 went with them. So we're gonna address that in just a second.
0:13 But let me go and copy over some HTML,
0:16 just like before. I'm just gonna use some standard HTML that's already here.
0:19 So, for example, when you just go to your account it says
0:21 "Welcome to your account", whatever your user is dot name and when you go register
0:27 it's gonna provide you a form that had, lets you type in your name
0:32 your email, your password and if there's an error,
0:36 it'll show you what that is.
0:37 OK, so this is a standard HTML form that we're gonna use,
0:41 and we're just going to go here and we'll do.
0:44 an @template(), let's see,
0:47 we've got account index, got account index.
0:49 I think we could just throw the template on like that and like that.
0:57 OK, so this is almost ready to work.
0:59 But if you go and actually look at,
1:02 say the index, it has a user which has a name and if we go
1:05 to our view model, it's not going to provide that data.
1:08 So what we're gonna get is that there's going to be a NameError,
1:10 so let's go quickly provide a user. So this user will probably come from the cookie
1:15 that we get, we'll get an id.
1:17 And if we have an id out of the cookies,
1:19 then we'll be able to pull them out of the database.
1:20 But for now, we're just gonna say the user is going to be a
1:24 new user, which we're gonna hack together in here.
1:28 And it takes the name, so I'll just put
1:30 Michael and michael@talkpython.fm,
1:34 and the hashed password is, whatever it is.
1:39 And we also need to call the base, like that.
1:47 OK, so this, let's double check that our
1:49 user has a dot name. They do, so this should work.
1:53 All right, now, let's go and give it a try.
1:55 We need a way to log into here and indicate whether or not we're logged in
1:58 But it looks like that is working in terms of the HTML.
2:02 That's cool. And let's go see about doing something similar for register.
2:06 If we look at register, we're gonna have to provide some information over to it
2:10 name, email, password.
2:13 We go to our view model and do the same thing.
2:16 def __init__, add the super() call like that.
2:22 And we're just gonna have a couple of strings.
2:23 So we'll have email, the string, an optional string because maybe somehow it didn't get
2:29 submitted. Gonna start out
2:31 None, and as a minute, at a minimum,
2:35 we have name, email, password,
2:39 and then the error is gonna come from the base class.
2:41 So there's nothing special we have to do there,
2:44 And let's see if we have everything we need. Name,
2:46 email, password, error. I think we do. Notice that we're also pulling in some
2:50 extra CSS to make this look good.
2:55 We go back and try to register. Tada!
2:57 Look at that. We also have age in years,
2:59 we don't really need to use that.
3:01 But it was there, just for some,
3:02 we're gonna use it just to show some of the client side validation in just
3:06 a little bit. All right. So we've got our name, we've got our email address and we got a password.
3:09 What happens if I fill this out?
3:14 I really love the password "a" So that's nice.
3:17 I'm gonna go with that. And my age in years is 18.
3:20 Click register, "Method Not Allowed". What is that?
3:25 Well, this is our POST,
3:27 remember when we went to it like this,
3:30 that's doing the GET. But when we pressed that button, it's submitting the form,
3:33 which is a POST. Now if we go back and look at PyCharm here, at
3:37 the view. What do we say? well we say, well we can handle an HTTP
3:40 GET right here to this URL. But where are we handling the HTML, the HTTP
3:46 POST? we're not. Remember the GET > POST > Redirect pattern.
3:51 So that's what we need, to add here.
3:53 When we do a GET, we're just gonna pass along the empty email,
3:57 empty name and so on. And then when we submit it,
4:00 we want to do something different.
4:02 So we're gonna have a totally different function if you're doing something like this.
4:06 Like if request dot method is GET do something, else
4:11 do, If it's POST, do something else.
4:14 No, that's doing it wrong.
4:16 You want separate functions because their job is entirely different.
4:20 One is to create the form.
4:21 Another is to create the user and then send them on their way.
4:25 So this one is going to be our POST.
4:29 Now let's just do a print really quick.
4:31 We'll say print "GET REGISTER", and "POST REGISTER" just to see that these are happening
4:38 because you won't really be able to tell until we do a little more work.
4:41 So let's just make sure. Here we've got that form.
4:46 We're doing our GET register. I'm gonna fill it out.
4:52 Register. Reloaded the form because we haven't changed anything about where it goes,
4:57 but notice that's the POST. So that's the GET > POST > Redirect pattern.
5:01 And what we're gonna do is we're gonna need to do a little bit of work
5:04 This is where it's gonna get interesting.
5:06 A little bit of work to grab the data from FastAPI,
5:10 to pass it over to the view model, to validate it,
5:12 to make sure everything is looking the way it should be.
5:15 And then, if everything is good,
5:17 we're gonna actually send them over to their,
5:19 /account details view up here.
5:22 If it doesn't work, we want to just tell them it didn't work,
5:24 here's why. Please try again.
5:26 But a lot of work to do here,
5:27 but we've implemented to GET, POST almost Redirect pattern.
5:31 We're going to do the Redirect later,
5:32 but here's how we handle the GET and POST submission of the form.