Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Getting the submitted values

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have our data submitted
0:01 to this register post action method here.
0:04 However we're doing nothing with it right?
0:07 So let's do something.
0:08 Let's actually create the user
0:09 and save them to the database.
0:12 Okay so what we're going to do in this chapter
0:15 is we're going to do kind of the ugly way of doing this.
0:18 And then we're going to improve dramatically
0:21 upon that when we get to the validation
0:23 and other sort of data handling things.
0:26 I'll show you the naive straight forward way
0:28 and then a better way in a minute.
0:31 Remember when we ran this we saw that
0:33 the POST has really the data that we're after.
0:37 And so what we're going to do is we're
0:38 going to get that data out here.
0:39 We'll say email equals request.Post.get('email')
0:45 We could of course get it out like this
0:48 but if it's not there for some reason
0:50 somehow this thing got submitted incorrectly.
0:52 Boom key error. So if we do it this way we can test and say
0:55 "No no the emails supposed to be there." something like that.
0:59 Alright so we could also get the name. We'll do the password.
1:07 So that gets the three pieces of data out
1:09 and then what we want to do is we want to check
1:13 hey is this correct? So we'll do something to this effect.
1:16 We'll say, if not email, or not name or not password for now.
1:24 Normally it would say if you didn't
1:25 set the email send a message emails required.
1:28 If the name... we'll do that later.
1:29 For now we'll just say error equals some
1:33 required fields are missing.
1:35 In this case we don't want to go on
1:37 and try to create the user
1:38 we want to just send them back to the form.
1:42 Over here though when we are... and here
1:44 we'll have a little... create user
1:48 and then we'll have a redirect.
1:50 So we'll just say return... lets go over
1:53 here and import HTTPExceptions as x
1:57 something like that right? Keep it short.
2:01 We'll do HTTPNotFound and it's going to be /account.
2:04 So once you register it's just going to
2:06 send you to your account page like
2:07 "Hey welcome new user."
2:10 so how do we communicate this error back to them?
2:13 What we want to do is lets actually add something over here.
2:18 And we have some piece of data
2:21 I'll have a DIV and the class will be error-message like that
2:25 We'll define that in a second and then we're going to put error
2:28 and we want to say only show the error section
2:33 if there is an error and of course we'd like that
2:35 to look error-like, right? So let's go to our site and say
2:43 Color is some kind of red. Let's darken it a tad.
2:49 Like that. If I run this you'll find it's not as amazing
2:53 as it seems like it's going to be. See why. You go here.
2:57 Register. Boom. No. Error. Where did that come from?
3:00 Well that came from the GET. That came from not here
3:05 but over here. See what we need to do is we need to
3:07 say errors None. Again we're going to make this better shortly.
3:14 So here we'll say error is the error message.
3:17 We can fact in line that if we want like so.
3:21 Let's try again but sure to rerun. Good. If I submit this
3:26 some required fields are missing. Well. That's pretty good
3:29 but watch this. If I say Michael and I say my email address
3:33 and I submit it. Everything is gone.
3:36 Do you know how frustrating that would be if this
3:37 was a big form with important stuff in it?
3:39 It's already a little frustrating. So what we need to do
3:42 is also carry that other information across so
3:45 email is you guessed it email and so on.
3:50 And for the same reason above
3:52 we need then pass these as None.
3:57 Great. Now the form actually needs to put that data back in
4:01 so lets go here to the end and say value equals $name
4:06 and similarly for all the rest. Password. Okay. Rerun it.
4:12 All this roundtripping should be working.
4:14 If I put Michael and my email address and I enter.
4:18 Look it stays. Very nice. But some errors are there.
4:21 So let's look back over here real quick.
4:24 It's coming through but first we're
4:26 pre-populating it with nothing.
4:27 You could put values if you wanted but if there's an error
4:30 we're actually going to not create the user.
4:33 Carry that information back and send the error message
4:36 in this case. And that's what just happened.
4:39 So let's just see what happens if we make it through.
4:41 We're not actually creating the user yet
4:42 but if we make it through. Boom.
4:45 Now we're in the account page. We passed the validation.
4:48 Now the step is going to be to come down here
4:50 and actually do something with this.