Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept: GET-POST-Redirect pattern
0:04 There is one basic flow or pattern that you need
0:07 to understand to work effectively with forms,
0:10 and once you get the zen of this idea, forms are piece of cake,
0:13 so let's talk about this pattern I am calling GET/POST/redirect.
0:17 So imagine we have our app running up on DigitalOcean,
0:20 our application has a database, things like that;
0:23 now, a user comes along and maybe they want to register for the site,
0:27 so they are going to come along
0:29 and they are going to do a GET request to accounts/register.
0:32 And that's going to bring down the HTML, the form that you saw in the previous video
0:36 that has the inputs and buttons and actions and all that sort of stuff.
0:40 The user is going to edit this locally, and then once they are ready,
0:44 they are going to post that data, that form back to the server.
0:49 That's going to come back to an action method on one of our controllers,
0:53 we are going to look at that data and decide either it's valid or not valid,
0:57 assuming everything works, we are going to save that validated data to the database,
1:01 and then, we don't want to just leave them on the "create your account page",
1:05 we want to take them somewhere, maybe we want to take them to our catalogue
1:08 or we want to take them to their "account page"
1:11 where they can set their picture or whatever,
1:13 but we are going to do a redirect, an HTTP code 3 or 2 redirect,
1:18 in this case we are going to redirect them to a "welcome page"
1:21 that helps them get started with our site.
1:23 So this is how almost all forms work, GET, to get the original form, you edit it,
1:28 POST back usually the same URL, not required but is helpful,
1:32 and then if everything is good, we are going to make some changes and redirect,
1:35 to some new place.
1:38 If we were adding a book to a book store, we might redirect to the details of the book,
1:41 or maybe we'd redirect to the listing of all books in the bookstore
1:44 showing the new one at the top, something like that.
1:47 Now I just made up this pattern, it seems very common to me
1:50 and it really helped me be effective, understand how to work with forms.
1:53 It turns out, other people came up with this similar pattern as well
1:57 and they put it on Wikipedia, however, I think it's a little bit different
2:01 it feels like they are jumping in the middle of some kind of experience
2:04 because they call it POST/redirect/GET, you magically have a form,
2:08 and then you somehow post it, redirect to a place and that redirect causes the GET,
2:12 so here it is on Wikipedia, known as POST/redirect/GET,
2:16 I think the ordering more like GET/POST/redirect, you can call it whatever you want.