Full Web Apps with FastAPI Transcripts
Chapter: Course conclusion and review
Lecture: GET/POST/Redirect pattern

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next thing that we looked at was, how do we get users to register or log in to the website? We talked about a lot of interesting things like,
0:08 how should we structure our User class? And what kind of validation should we have? And how do we store it with things like passlib and so on?
0:14 But one of the important takeaways, the more broad lesson from that chapter really was about: How do I create an HTML
0:22 form that I let the user type into, with validation that they can then submit back
0:27 to the site? The starting point for that was understanding we're probably best off using
0:32 this GET > POST > Redirect pattern, and it's incredibly easy to do in FastAPI. So we're gonna set up a page that we GET,
0:41 that's the empty form, we're gonna fill it out like, hey, click here to register for the site and the empty form fills up, shows
0:47 up. We fill it out by editing it locally, and then we want to save it.
0:51 Now, assuming that we passed all the local validation like the HTML5 required and is an email and so on, we're gonna submit that through an HTTP POST,
1:01 that's how forms are submitted to a different method in our website. we put the @app.get() on the first one, we put @app.post() on the second one,
1:11 even though they have the same URL. Over there we used our view model to validate and convert our data,
1:16 if everything worked, we saved that back to the database, and then we issue a 302 Redirect. Remember, with FastAPI,
1:23 a regular redirect keeps the POST behavior, the POST verb flowing. So we wanted to issue a 302, it's over here, go do a GET request to that,
1:31 which was the more standard web behavior that you would expect. If this didn't work,
1:36 we just have the view model return the data that they had entered back to the form by returning the dictionary with the same template.
1:43 Easy as it can be. This is the way almost every HTML form, that's not some kind of JavaScript single page
1:49 app driven type of thing, this is how it works and how it's easiest to implement in FastAPI.

Talk Python's Mastodon Michael Kennedy's Mastodon