Building Data-Driven Web Apps with Flask 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 Before we actually start writing some code
0:01 let's talk about this thing
0:03 called the get/post/redirect pattern.
0:06 Now this is a super common pattern
0:08 that you see on server side web frameworks
0:10 and it almost entirely eliminates that weird problem
0:13 where you see things like
0:15 Hey, this forum has been submitted
0:17 Are you sure you want to resubmit it?
0:18 If you refresh the destination page
0:21 or other weird stuff like that.
0:22 So it's a really nice pattern, its super clear
0:25 and its easy to implement in HTML and in Flask.
0:29 So let's go through the concept here
0:31 so our server side we've got our server
0:33 and we've got a database
0:34 and then some body wants to talk to our website
0:37 so here they are on their laptop running
0:39 whatever browser they run
0:41 and what they going to do is they going to go
0:43 and request a page on our site.
0:46 So let's imagine they went to register
0:48 so they will go over here and they'll do an HTTP GET
0:50 that is just click on the link for /accounts/register
0:53 and that page will display an HTML form
0:57 it'll ask them questions like What is your name?
1:00 What is your email address?
1:01 What password do you want to use?
1:03 so your going to type that out and edit it locally.
1:05 So they've done the get and they're making their changes
1:07 and when they are ready to register
1:09 they click submit on that form.
1:11 As we saw that's often a POST so it's going to do a HTTP POST
1:15 with that form data back to the server
1:17 will do a little validation and says Super, new user
1:19 I save them to the database.
1:21 Now what what do we tell this user?
1:24 Do we just leave them on this register page?
1:26 No, we want to take them somewhere
1:27 to their account page, to some kind of welcome to our site.
1:31 So we're going to send them a 302 redirect
1:33 a temporary redirect to /welcome.
1:36 And that's it that's the get/post/redirect pattern
1:38 we do a GET to get the form, we edit it
1:40 we do a POST to submit that data, to save it
1:43 and then the server does a redirect
1:45 to where you actually want you to be.
1:47 And on the /welcome if we hit refresh it doesn't go
1:50 Warning! Warning! You submitted a form.
1:52 No, it just goes, Alright well we'll refresh the welcome page.
1:55 Really, really nice pattern, its super easy to implement
1:57 in Flask as we will see
1:59 and I totally recommend you adopt this.
2:01 Now if we go look around like say on Wikipedia
2:04 you'll find some thing like this but named differently
2:07 they call it the post/redirect/get pattern
2:10 so you can look that up here, the URL at the bottom.
2:13 I don't like it because the thing
2:15 that starts the whole process is not the POST
2:19 the thing that starts this process is getting the form
2:22 you fill it out and then you do the POST
2:24 and then you do the redirect, hence get, post, redirect.
2:27 They call it post, redirect, get.
2:29 I guess it depends on where you put the focus
2:31 but it's effectively the same pattern
2:33 and you can learn more about it here on Wikipedia.