Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Register on the site (handle errors section) demo

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


Talk Python's Mastodon Michael Kennedy's Mastodon