Full Web Apps with FastAPI Transcripts
Chapter: Users and HTML forms
Lecture: Redirecting after registering
0:00 Well, we've got our form taking the data and validating it, but it just stays right here.
0:06 So there's a couple of things we still need to do to make this work. One is we need to go over here and say TODO:,
0:12 Create the account, and let's go and write some code to do it and then we'll make this actually happen when we get to the database.
0:19 This is gonna be the user_service, and we'll say create_account() and we need to pass all that information over. So view model is going
0:29 to have the name, the email and the password. That's going to create an account and we probably also want to say TODO: Login user,
0:43 which we still got to get to in a moment, do that. And then we want to do the redirect, right? We're still kind of focused on this
0:51 GET > POST > Redirect and this the last step in that. Let's go ahead and write that function real quick so it doesn't crash,
0:57 it's just gonna be strings and it's gonna return one of those users, one of these classes, and we'll just have it return
1:11 the user like this for a minute name, email, "abc". We've gotta hash the password, not for the moment, but we'll get back to that.
1:20 OK, So we're gonna log in our user and then we want to redirect. Now, remember we talked briefly about FastAPI responses, responses there.
1:29 There's a bunch of great stuff, we started out by saying, oh, there's this regular response and the JSON response,
1:34 and let's use an HTML response for a minute. You can see these actually come out of Starlette, not FastAPI directly.
1:42 So we've got a perfect candidate for our redirect. We're gonna return a RedirectResponse, and what could we put in there?
1:49 the url, the url is going to be equal to /account. Let's give this a shot and see what happens. All right, come over here.
1:58 We're going to submit this form, it's gonna fake out creating that account, and then it's gonna redirect to /account.
2:04 But it's not gonna work the way you're expecting. You probably think we'll just see the account page shown here.
2:08 But let's see what happens. Method not allowed. Wait a minute, what just happened? what just happened? Look at this,
2:16 it did an HTTP POST when we did the redirect from /register it redirected as
2:23 a POST. Most frameworks automatically convert redirects to GET, FastAPI and Starlette do not. So no problem,
2:33 what we can do is, we can go over here and we can actually import status. We can set the status_code equal to status, and this is going to come
2:41 out of Starlette. And here we have all the HTTP statuses and the one we want is 302 Found, and that means redirect with a GET. Right, one more time.
2:53 Go back here, try to submit my form. Should see it, redirect, and off it goes.
2:59 Tada! there we go. And we've got the correct redirect instead of one of those 307 or whatever it was there. 302 Found when I did the
3:06 POST to register and then it did the GET to the account. So GET > POST > Redirect. Perfect, so now we've got this working and you can imagine something
3:16 very, very similar for login, right? We're gonna create a GET version, a POST version. The view model is gonna load the data out of the form,
3:25 validate it, do some stuff like log in the user, make sure their account exists, log them in and then do a redirect, probably over to the same place as
3:33 well. So this pattern will serve us for most of the HTML forms that we wanna work with.