Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: Introducing the Pyramid framework
Lecture: Building block: Routes and URLs

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next major building block to cover is routing,
0:02 or routing, if you prefer British English.
0:05 Routes allow us to define patterns that match
0:08 some URL and map over to a particular view
0:13 or controller method.
0:15 Sometimes that just says, "This URL calls this function,"
0:18 other times it says, "This URL contains Data,"
0:21 so like /package/request or /package/sqlalchemy
0:27 would pass the request or SQLAlchemy to that function.
0:31 So, let's see how we do this in Pyramid.
0:33 So, we're going to go to the main method
0:35 in the file
0:36 that is basically the entry point or the start up
0:39 for our web application, or we're going to
0:41 start with this thing that's a configuration file reader.
0:45 On there, it has a function called add_static_view.
0:48 So here, we're naming the route static,
0:50 and saying it maps to /static or anything under there.
0:54 So, what we can do is we can go here and say
0:56 anything in this directory or below it,
0:59 will be cached by a quick little calculation.
1:03 It's one month, so you've got to put an integer
1:05 that is the number of seconds to cache it for,
1:07 I like to put this as calculation so I can see
1:10 60 seconds, 60 minutes, 24 hours a day, 31 days,
1:13 okay, that's a month.
1:15 Then we're going to define the routes
1:17 that are the various URLs
1:19 that go to our action methods.
1:22 So the first one is /, just like that, and that's home.
1:26 Right, that's just the basically the main page
1:29 when you hit the site.
1:31 And we want to have a /help
1:32 so add a route for that for help.
1:35 Then we're going to have, in our PyPI example,
1:38 the route that they use for an individual package
1:41 or the URL is /project/{package_name}.
1:45 Now notice, this one is different.
1:46 It has package_name as a curly bracket
1:50 and then a variable looking thing.
1:53 package_name will actually be passed to the function
1:56 that we apply this to.
1:57 So, like I explained before, SQLAlchemy, requests, whatever
2:03 so the URL itself will carry the information along
2:05 and that's defined in the route here.
2:08 Also, we have things like account/login.
2:10 And the final action to do is to tell the system to go
2:14 figure out what functions are associated with these,
2:17 so we have the route name, we also need to have to have the
2:21 route to actually apply to this.
2:23 So we run the scans and it looks through all
2:25 the Python files, looks for that view_config decorator
2:29 using various route names and then it
2:31 puts those two things together.
2:34 And finally, we have to return this WSGI application,
2:37 W-S-G-I, WSGI, application that the web framework's
2:41 going to use and this is just at the end
2:43 of the main startup always.
2:45 So this is how you define routes in pyramid.