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