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