Full Web Apps with FastAPI Transcripts
Chapter: Course conclusion and review
Lecture: GET/POST/Redirect pattern
0:00 The next thing that we looked at was,
0:01 how do we get users to register or log in to the website?
0:05 We talked about a lot of interesting things like,
0:07 how should we structure our User class?
0:09 And what kind of validation should we have?
0:11 And how do we store it with things like passlib and so on?
0:13 But one of the important takeaways,
0:16 the more broad lesson from that chapter really was about: How do I create an HTML
0:21 form that I let the user type into, with validation that they can then submit back
0:26 to the site? The starting point for that was understanding we're probably best off using
0:31 this GET > POST > Redirect pattern, and it's incredibly easy to do in FastAPI.
0:36 So we're gonna set up a page that we GET,
0:40 that's the empty form, we're gonna fill it out like,
0:42 hey, click here to register for the site and the empty form fills up, shows
0:46 up. We fill it out by editing it locally,
0:48 and then we want to save it.
0:50 Now, assuming that we passed all the local validation like the HTML5 required and is
0:56 an email and so on, we're gonna submit that through an HTTP POST,
1:00 that's how forms are submitted to a different method in our website.
1:05 we put the @app.get() on the first one,
1:07 we put @app.post() on the second one,
1:10 even though they have the same URL. Over there
1:12 we used our view model to validate and convert our data,
1:15 if everything worked, we saved that back to the database,
1:18 and then we issue a 302 Redirect.
1:20 Remember, with FastAPI,
1:22 a regular redirect keeps the POST behavior, the POST verb flowing. So we wanted to issue
1:26 a 302, it's over here,
1:29 go do a GET request to that,
1:30 which was the more standard web behavior that you would expect. If this didn't work,
1:35 we just have the view model return the data that they had entered back to the
1:38 form by returning the dictionary with the same template.
1:42 Easy as it can be. This is the way almost every HTML form,
1:48 app driven type of thing, this is how it works and how it's easiest to implement in FastAPI.