Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Reorganizing the app
0:00 Go work on the routing now.
0:02 We've got this main method.
0:03 Now, this is going to start to get complicated, in fact
0:07 as you get these real apps filled out
0:09 I think in Talk Python, possibly this might be 150 lines.
0:14 I don't know.
0:15 It's more than just 12, 13 lines of code.
0:17 So let's do a little refactoring here.
0:19 Let's take the stuff to do with the routing
0:21 and make it it's own little method.
0:23 So that part, we can come over here.
0:24 Say right click, refactor, extract, method.
0:29 Of course there's hot keys for all of this
0:31 which I normally use.
0:33 And I'll just call this init_routing.
0:36 OK, let's get a little room.
0:38 Let's keep going here; let's do this one as well.
0:44 There's includes.
0:46 Alright, looks pretty good.
0:47 We'll add some stuff about databases
0:49 and other things as we go.
0:52 Alright so down here with this
0:53 well let us just focus on the routing.
0:56 So far we have two routes
0:58 mapping to two viewer action methods
1:00 and those are over in here.
1:02 So this, I'm not a huge fan of this.
1:05 This is all kind of crammed together into one thing
1:07 and well they're kind of related, home and index home about.
1:10 Whatever. Let's put that home prefix here as well.
1:14 Remember, keep these in sync, and you'll be happy.
1:17 Of course we got that.
1:18 Alright, so let's do this.
1:19 Let's create a controllers folder.
1:26 Careful here. Notice I said, "create directory."
1:28 That is actually going to make this part likely fail.
1:32 So what that does is that loops through all the files
1:34 in this package and subpackages.
1:37 What we need to do is create a subpackage.
1:39 Now, packages in Python can be mysterious.
1:41 They're quite simple in this context.
1:43 I'm going to call this controllers.
1:48 And what it does is simply make a directory
1:49 with an empty __init__.py.
1:52 Okay, so that's all we got to do to tell Python that
1:56 "Hey, there's this thing going on here."
1:57 So let's move that over.
2:00 And let's rename that to HomeController.
2:08 Let's see that things still work.
2:09 And again we want to tell the single s's only.
2:17 But clicking on it, problems.
2:19 So this, basically, is not working for us
2:23 we need to change this just the tiniest bit.
2:26 So what we need to do is go over here
2:28 and put the package name, just say
2:30 "Go to the top," and look.
2:31 So PyPI: right here.
2:34 And PyPI: right there.
2:36 Python says that's misspelled; I say, "Learn it."
2:39 Okay, try one more time.
2:42 Try it again.
2:43 Hey, hey, it's working again!
2:45 Alright, so everything is good.
2:49 We've got our organization going here.
2:51 And the templates, let's go ahead
2:52 and organize these as well.
2:57 So, create one called home.
3:00 And we're going to have packages.
3:02 We're going to have a PackageController.
3:03 I'll just create one called packages here as well.
3:06 And finally, we're going to have some account stuff
3:09 in our route. So let's have account.
3:12 Alright. Oh, and also, don't forget, shared.
3:15 So, you might be saying
3:17 "Michael, what does this have to do with routing?"
3:18 Well, it technically has nothing to do with routing exactly
3:22 but as we're adding these routes
3:23 I want to make sure we're putting the structure in place
3:25 so this doesn't become a mess.
3:26 Because we have to have controllers for the routes
3:28 which means we got to have the layouts, and so on.
3:31 Alright, quick reorg.
3:38 Over here we can probably drop the name home.
3:47 Of course, one more time, we've got to fix these up.
3:49 This has to be home.
3:52 That has to be home.
3:53 And then in our various things up here
3:56 this has to be ../shared/layout.
4:05 Let's rerun it and try one more time
4:07 make sure everything's good.
4:09 Alright, looks like it's hanging together.
4:11 Click the various bits.
4:13 Okay, all good.