Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Register POST action
0:00 Now our form is looking good
0:01 but to submit it back we have a little bit more work to do.
0:04 Let's go over here.
0:06 When the browser takes these forms
0:08 and it submits it back to the server
0:10 what it does is it takes the name
0:12 not the id but the name, of these various elements
0:14 and sets those up as a key value pair.
0:16 So we have to make sure we set the name of each of these.
0:19 And this one is just, name is name
0:21 like your name, so would be name equals email.
0:24 Right, now with that in place what we can do
0:26 is we can go over here and actually
0:28 see the data that's coming in.
0:30 At the top we have flask imported
0:32 and what we're going to do is we want to get to the request.
0:35 So we'll say, r equals flask.request.
0:39 Like so, lowercase one.
0:41 And then let's just for a moment
0:42 print out the various values.
0:44 So, we could go to r. and we have a couple of places.
0:48 If we have a query string, that is the question mark
0:50 keyword value stuff in the n way of args
0:53 we form, we also, we saw if there's data
0:57 right like slash mode, right, mode is edit
1:02 new user or whatever, we pass that in here
1:05 as a string, str whatever it happens to be.
1:09 So that gets passed in this way from the URL
1:13 not having any of that.
1:15 And then the other one that's a little more
1:16 subtle is the headers, so we could have query string
1:19 we could have the form, we could have the actual arguments
1:21 and we could have the headers.
1:23 So let's just for now print out r.form
1:25 and see what's coming in.
1:29 And let's just put some various info in here.
1:31 All right, if we submit this form what do we get
1:34 let's look over here. Uh-huh, perfect
1:35 we get a immutable dict, with a name as Michael
1:39 Emails at our values from our form
1:41 are all been passing here, just like we'd expect.
1:44 So lets go ahead and get those values real quick
1:45 so we'll say something to like the effect
1:48 of name equals r.form.get.name
1:56 all is in now let's do a little bit extra here
1:59 so maybe we don't want to deal with, values are maybe there
2:02 fine but you going to normalize the email
2:04 so we can put a default value of empty string
2:06 if nothing comes in here then we can say lower.strip
2:10 like this, that way when we save it to the data base
2:13 regardless whether they type in upper case email
2:15 or lowercase it's always going to be the same.
2:17 And then maybe here we just do a strip
2:20 maybe we don't want to allow spaces
2:21 on the end of the password.
2:23 So let's just see how this works one more time.
2:25 Okay, type this out again one more time.
2:29 So what we get, we get the name is Michael
2:33 the email is that, and the password this time was abc.
2:37 All right it looks like we're getting our data from the form
2:39 now it's a matter of actually
2:40 doing something interesting with it.