Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Register on the site (redirect or error section) demo
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So now we have this data being passed in, we would do something... let's comment that out for a second, we would do something
0:08
like maybe over here we would create account in DB we would send welcome email,
0:16
before we did any of that of course we want to validate no account exists and maybe passwords match, although you know this confirmation business
0:26
I know a lot of sites have it and it feels great but at the same time if you have a really good way to reset your password,
0:32
then maybe we could simplify and streamline the account creation and maybe get just a few more percent people to actually sign up for our site,
0:41
that would be really nice so we can decide if we want this in practice, but these are the types of things,
0:45
validate that the data is OK, that they actually entered an email the email is valid things like this and then if it is, we'll create an account,
0:52
I'll send a welcome email and then what we want to do is we want to redirect. We can just raise an exception, a specific exception type.
1:01
Now this redirect pattern, I told you get POST/redirect is super common, so the ability to redirect to a URL is also something you need all the time,
1:09
where the stuff that all the action methods need go? It goes in the base controller, so let's write a redirect function over here.
1:17
So here we'll just have "def redirect", and then we'll have to_url, where do we want them to go?
1:23
It could be on our site, it could be somewhere else, it just depends what URL we put. OK, we are going to need access to this exception thing,
1:30
so let's go over here and say import pyramid.httpexceptions and let's keep it a little bit short, so let's say "as exc",
1:38
so we don't have to type out that long thing, and then down here we are going to actually add one more parameter,
1:43
let's say whether let's let the user decide whether or not this is a permanent redirect, sometimes you want to have a temporary redirect,
1:51
like our login for example, other times, you are going to permanently do a redirect and this is important for SEO, we won't get into it now
1:59
but let's go ahead and set that ability here. So we are going to return or cause one of two types of exceptions,
2:05
we'll say "if permanent", we want to raise our exc.HTTPMovedPermanently.
2:12
Now notice we can set the location, we can set the details, the headers, comments,
2:17
all sorts of stuff but we are just going to say the location is the to_url.
2:22
But if it's not permanent, we are going to raise the different type of exception,
2:25
HTTPFound, so I know you are looking for this but I found it over there so go redirect over to this to_url.
2:33
So PyCharm is still in this, this can be static, let's just tell it, hey don't bother me. Don't make it look like there is something wrong with this,
2:40
so back to our account controller, we're ready to redirect, assuming that we get through all this validation,
2:45
we don't have to return anything, we can just say self.redirect because remember, this throws an exception, where are we going to go?
2:51
Let's just go to /account. And this is not going to be a permanent redirect, this is just when you log in.
2:58
So, let's do this, we can put this back for a minute, and we can print out redirecting, alright,
3:05
so we pretty much have the GET/POST/redirect thing in place, and here is the GET, the user is going to post it back, the post is going to hit this one,
3:12
we'll do the various work, which we are not doing the validation yet, we'll do that in just a moment, then we are going to redirect here.
3:19
Let's rerun the app and check it out, so let's go register, my name is going to be "jeff", the password is going to be "a", again "a", hit Enter,
3:28
calling this we have two passwords, redirecting our account page and where are we? We are now on /account, not /account/register, just /account,
3:35
because guess what we register for the site, how awesome is that? Ok, last thing we are going to talk about after this is
3:42
how do we add a little basically temporary validation and we'll see a pattern that makes all of this cleaner.