Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Register on the site (handle errors section) demo
0:00 So the next thing we need to do is deal with the case
0:03 where the account already exists, or the passwords don't match
0:06 and let's just do this by assuming we are just going to test for the password to match
0:09 and I'll trigger it either match or not match
0:12 so we'll do something like this, we'll say
0:13 "if password is not equal to password confirmation", now what?
0:18 Well, we want to stay in this form, right, we stay in this form by saying this,
0:24 so we stay in the form by just returning it and up here we have a renderer,
0:29 it's going to render that form again, but the data that we put in here
0:33 is what we want to well supply to the view,
0:35 and the view can do whatever it wants to do with it.
0:38 But likely what it wants to do is tell the user there is an error
0:41 and make sure that all the data that entered is still there,
0:43 so let's carry the data that they entered over,
0:46 so I had email and I'll just fill this out, so instead of redirecting away,
0:54 what we are going to do is we just want to send this data back
0:57 but really we need one more thing, we don't want to just to try to submit
1:00 the form and have it bounce back, we want to say: "Hey,
1:02 you know what? Your password does not match."
1:04 So let's add one more additional field here, called "error".
1:08 There, so we are going to have the email and password, confirm password and so on,
1:12 let's update the form to put those in the value,
1:20 and finally let's go ahead and show the error,
1:22 we'll say up here we'll say something like a div,
1:26 and it's going to be error, now of course we don't actually want to show this error
1:30 when there is no error, do we?
1:32 So let's give this a class of error so the error message,
1:36 so we can make it red or stand out or something,
1:37 and we'll do a tal condition and only show it if the truthiness of error is True,
1:44 so it's a string that is not empty, something like that.
1:48 Let's add a quick red color for that CSS class,
1:51 now this is not going to work the way you would hope, I think, let's go and run it.
1:56 And we'll see how successful we've been. Spoiler alert, not successful yet.
2:00 So I go over here and I want to register,
2:02 now I've got all this ability to carry on and show the message
2:05 and resume the data that they passed in, and yet, not so awesome.
2:09 What's the problem? hm, NameError, there is no error,
2:13 the initial GET did not supply the right dictionary
2:17 and you'll see that we have a nice clean way to solve this but just for this step,
2:20 before we get to that too much, we need to go over here
2:23 and just grab this and in our GET we need to supply the same information,
2:29 of course these are all None, they are None but they must exist
2:34 when we try to access them out of the dictionary that is the model,
2:38 let's try one more time. OK, great, it didn't crash, let's put this email as jeff
2:44 and I'll put this as the password, now I'll try to hit this, see what happens, boom,
2:49 how nice does that look, "Error: The password and confirmation don't match",
2:53 and you can see obviously it’s the in password,
2:57 maybe I want them to be "the password" and "the password",
3:01 let's see if I actually typed that right, hopefully, I intended this to match, let's try.
3:05 Boom, they did match, so we did the redirect.
3:08 Alright, you see how that works?
3:11 Over here we have our error, we have the various pieces of data,
3:15 like the email and password and so on that we wanted to pass in, and in the controller,
3:19 we have our GET/POST/redirect pattern and GET sets the initial data,
3:23 sometimes it's empty, it could be a drop down list, it was populated,
3:27 who knows what it is, but it has to exist, then we are going to work through here
3:31 we'll handle the post, if it's successful we just go and do the things we do in redirect,
3:36 if it's not, we are going to return the data that they gave us
3:39 as well as some kind of error that they can use to understand what they did wrong.