Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept: GET-POST-Redirect pattern

Login or purchase this course to watch this video and the rest of the course contents.
0:04 There is one basic flow or pattern that you need to understand to work effectively with forms,
0:11 and once you get the zen of this idea, forms are piece of cake, so let's talk about this pattern I am calling GET/POST/redirect.
0:18 So imagine we have our app running up on DigitalOcean, our application has a database, things like that;
0:24 now, a user comes along and maybe they want to register for the site, so they are going to come along
0:30 and they are going to do a GET request to accounts/register. And that's going to bring down the HTML, the form that you saw in the previous video
0:37 that has the inputs and buttons and actions and all that sort of stuff. The user is going to edit this locally, and then once they are ready,
0:45 they are going to post that data, that form back to the server. That's going to come back to an action method on one of our controllers,
0:54 we are going to look at that data and decide either it's valid or not valid,
0:58 assuming everything works, we are going to save that validated data to the database,
1:02 and then, we don't want to just leave them on the "create your account page",
1:06 we want to take them somewhere, maybe we want to take them to our catalogue or we want to take them to their "account page"
1:12 where they can set their picture or whatever, but we are going to do a redirect, an HTTP code 3 or 2 redirect,
1:19 in this case we are going to redirect them to a "welcome page" that helps them get started with our site.
1:24 So this is how almost all forms work, GET, to get the original form, you edit it, POST back usually the same URL, not required but is helpful,
1:33 and then if everything is good, we are going to make some changes and redirect, to some new place.
1:39 If we were adding a book to a book store, we might redirect to the details of the book,
1:42 or maybe we'd redirect to the listing of all books in the bookstore showing the new one at the top, something like that.
1:48 Now I just made up this pattern, it seems very common to me and it really helped me be effective, understand how to work with forms.
1:54 It turns out, other people came up with this similar pattern as well and they put it on Wikipedia, however, I think it's a little bit different
2:02 it feels like they are jumping in the middle of some kind of experience because they call it POST/redirect/GET, you magically have a form,
2:09 and then you somehow post it, redirect to a place and that redirect causes the GET, so here it is on Wikipedia, known as POST/redirect/GET,
2:17 I think the ordering more like GET/POST/redirect, you can call it whatever you want.

Talk Python's Mastodon Michael Kennedy's Mastodon