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