Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Concept: GET-POST-Redirect pattern

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We went through several cycles
0:01 of creating these GET
0:03 and then POST view or action methods
0:07 while building out our user interaction in the site.
0:10 So let's review some of those core concepts.
0:13 Whenever we're going to start we typically start
0:15 with a GET action. Because we're going to have to
0:18 show the form to the user before they can submit it, right?
0:21 If this was like a RESTful API maybe that's not the case
0:24 but this is a website so we've got to show
0:26 the data, the form and any initial data to the user
0:29 so they can submit it back. So how's that work?
0:33 Well, we're going to set the GET request method
0:36 and we're also going to set the route name to a shared URL.
0:40 Right, so this is register.
0:42 It's going to be registered for GET
0:44 and it's going to be registered for POST.
0:46 Same thing with the template.
0:48 Then we need to make sure we have a unique function name.
0:52 I like to just call it, register_get, _post, whatever.
0:57 It doesn't have to be named that way, but it must have
0:59 a unique name that's not the same as POST or the other verbs
1:02 or it's going to erase it. Right?
1:04 Python doesn't give you an error when you do that.
1:06 It just takes the last one you typed in.
1:09 And finally, we're going to pass any data the form needs
1:12 Really it's going to exchange. So we're going to pass
1:14 the email address, the password, and even whether or not
1:18 there's an error. These are typically empty
1:19 but if you have things like comboboxes and dropdowns
1:22 and things like that, radio buttons, you might
1:24 pass those along to initially populate the form.
1:28 So this is the GET action, this shows the form to the user.
1:31 They do some editing, then they are going to submit it back.
1:34 In that case it's going to hit the POST action.
1:37 So again, we're going to set the verb, this time to POST
1:41 and we're going to use this shared route name.
1:44 Again here's register for both the template or the render
1:47 and the route name. We have our unique function name
1:50 this time registe_post.
1:53 We have our data in this Post dictionary so we pull it out
1:58 and we use the get instead of key access.
2:00 That's a little bit safer.
2:01 This is just get it and check to see whether it was passed
2:05 and we're going to do some validation, this is super important.
2:07 If it doesn't work pass that data back
2:09 with some kind of error message. If it does
2:11 you're going to do whatever processing you do here
2:15 and then probably redirect them.
2:16 This is the final part of the get post redirect pattern.