Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: GET-POST-Redirect pattern

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now before we write some code let's talk a little bit about the flow that people go through when they're working with forms.
0:06 There's many ways to do this but there's one pattern that really helps both organize your server side code and prevent that annoying warning like
0:15 "are you sure you want to refresh this page? "You've posted it, it might resubmit "your purchase request" or whatever.
0:23 I'm calling this the Get Post Redirect pattern. So what's that look like? Well, we have of course our server. Our server has some sort of data.
0:32 We have a web browser, and our web browser is going to interact with our site. It's going to say, "Hey, I want to view this HTML page
0:38 "that happens to have a form "like I want to register for your site." And then over here locally, they're like "All right, great.
0:44 "Here's my username, here's my password, my name," whatever you got to do to register. They type that in and they click that submit button
0:53 we just talked about. That posts all that data back and they're like, "Great, we've got a new account.
0:57 "We want to create it, we're going to save that to the database." Now we're going to tell the browser to move over
1:03 to another page, redirect, send a temporary redirect 302 over to /welcome, and welcome to the site. That way, if they bookmark it
1:13 or they refresh it or whatever they're going to end up on welcome having already created it not being left on the same URL where a POST just happened.
1:23 Like I said, if you refreshed that you would get a warning saying "Hey, this page is going to resubmit "Are you sure you want to do that?"
1:30 So this Get Post Redirect pattern is super common and it's really nice on the server side. I'll show you how to break that out
1:37 into different methods nice and easy so one handles the get, one handles the post. So one handles the show the form pre-populated with starter data.
1:47 The other processes that input and keeps the code really clean. You don't have to test whether it's being submitted or not.
1:53 This common pattern is sometimes known under a different name, the Post Redirect Get pattern. So over here on Wikipedia, they have an article on it.
2:03 You can read more about it as you want. To me, you have to start from somewhere and you start from the get, and then you do the post
2:11 and then you do the redirect. That's the life cycle of that form interaction. So I think that's why I call it Get Post Redirect
2:17 but Wikipedia, they have a different name. It's fine. It's a pretty common pattern and you can get more information about it here.


Talk Python's Mastodon Michael Kennedy's Mastodon