Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Account routes and views
0:00 The last thing we're going to do
0:01 is we're going to have the ability to login and out
0:03 and basically manage accounts.
0:05 You've noticed, I've already created that part there.
0:07 Let's go and add the routes for that
0:10 and we'll figure out what we need.
0:12 So, here we'll say account_controller
0:16 just for time sake, let me just drop those in there.
0:20 We're going to have /account, account_login
0:24 account_register and account_logout.
0:27 Go over here.
0:28 I'll implement those real quick and we'll come back to it.
0:32 OK, I've just dropped in a few of the things
0:34 that we're going to need that correspond to account
0:37 account_login, account_register, account_logout.
0:40 Over here we've got, here's just going to be /account
0:44 this is going to be you're home, and notice, in this one
0:47 I'm explicitly setting the request method.
0:50 I really only want this to respond to GET
0:52 and this one, actually, it doesn't matter so much
0:54 but down here it definitely does.
0:56 So, here's the login that's going to use the template login
1:00 which I've created over here
1:02 and this one responds only to GET.
1:04 This is going to show the form
1:06 whereas this one is going to process the login
1:08 when you submit it as an HTTP POST.
1:11 So we'll set the name, because we can't have two functions
1:15 with the same name
1:16 so we're going to have to name the route there.
1:20 With registration, same thing, register_get, register_post
1:24 and finally just to logout.
1:25 So that rounds out all the URL's
1:28 that are going to be at play here.
1:30 Let's run this, and just double check.
1:33 So, let's go over here to /account/login
1:40 All right, looks like the account stuffs working.
1:43 Trust me, I think the rest is going to be just fine as well.
1:46 That was a lot of juggling of various pieces
1:49 and I find that to be one of the things that can be tricky
1:52 routing, because we've got these view methods
1:55 that are contained within the controllers.
1:57 The controllers then link, you know
2:00 leverage these templates
2:02 and the templates are then laid out in a certain way.
2:04 Pretty straight forward.
2:05 What we've done is we've registered our core home routes
2:09 the various ones to do with packages
2:10 that we put in to the PackagesController
2:12 the one to do with accounts
2:14 put them in the AccountController
2:16 and then finally, we run scan
2:17 and it just goes through every file
2:19 looking for @view_config decorators
2:22 and then lines them up.
2:23 We saw that if they don't match
2:25 there's going to be an error.