Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Register POST action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now our form is looking good but to submit it back we have a little bit more work to do. Let's go over here. When the browser takes these forms
0:09 and it submits it back to the server what it does is it takes the name not the id but the name, of these various elements
0:15 and sets those up as a key value pair. So we have to make sure we set the name of each of these. And this one is just, name is name
0:22 like your name, so would be name equals email. Right, now with that in place what we can do is we can go over here and actually
0:29 see the data that's coming in. At the top we have flask imported and what we're going to do is we want to get to the request.
0:36 So we'll say, r equals flask.request. Like so, lowercase one. And then let's just for a moment print out the various values.
0:45 So, we could go to r. and we have a couple of places. If we have a query string, that is the question mark keyword value stuff in the n way of args
0:54 we form, we also, we saw if there's data right like slash mode, right, mode is edit new user or whatever, we pass that in here
1:06 as a string, str whatever it happens to be. So that gets passed in this way from the URL not having any of that.
1:16 And then the other one that's a little more subtle is the headers, so we could have query string
1:20 we could have the form, we could have the actual arguments and we could have the headers. So let's just for now print out r.form
1:26 and see what's coming in. And let's just put some various info in here. All right, if we submit this form what do we get
1:35 let's look over here. Uh-huh, perfect we get a immutable dict, with a name as Michael Emails at our values from our form
1:42 are all been passing here, just like we'd expect. So lets go ahead and get those values real quick so we'll say something to like the effect
1:49 of name equals r.form.get.name all is in now let's do a little bit extra here so maybe we don't want to deal with, values are maybe there
2:03 fine but you going to normalize the email so we can put a default value of empty string if nothing comes in here then we can say lower.strip
2:11 like this, that way when we save it to the data base regardless whether they type in upper case email or lowercase it's always going to be the same.
2:18 And then maybe here we just do a strip maybe we don't want to allow spaces on the end of the password. So let's just see how this works one more time.
2:26 Okay, type this out again one more time. So what we get, we get the name is Michael the email is that, and the password this time was abc.
2:38 All right it looks like we're getting our data from the form now it's a matter of actually doing something interesting with it.


Talk Python's Mastodon Michael Kennedy's Mastodon