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