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