Full Web Apps with FastAPI Transcripts
Chapter: Users and HTML forms
Lecture: Redirecting after registering

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