Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Routing overview

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Having properly structured URLs is super important for web applications. It matters for SEO, Search Engine Optimization
0:10 but it also matters for usability and discoverability of your users. I'm sure many of your users will be either typing into the URL bar
0:18 or looking at it and trying to make sense of the structure there so you want to have good control and put some thought into your URL structure.
0:28 And we're going to dig into the mechanism in Pyramid how we do that, how we create the best possible set of URLs and map those to our view methods.
0:40 So let's start by just talking about what is routing. We've mentioned it a few times and it's basically mapping URLs into action methods
0:49 or controller methods. So let's go through this. So a request comes in. This request is an HTTP GET, that's a verb, a HTTP verb
0:58 for /project/sqlalchemy. And imagine, over on the right we have our server and this sort of weird conceptualization
1:06 of the various view methods, or controller methods index and details to do with packages and index and about to do with home.
1:16 We've registered some routes in this application. We've got / and that goes to HomeController.index. And so the system's going to ask
1:25 Is /project/sqlalchemy a match for that? No. And in this case it has to be an exact match. There's no ambiguity or variability here.
1:36 So, no it's not that. We also have /about that goes to home.about. Now, also not a match, so we'll keep goin' The next one we have is /project.
1:48 And then that last part can change, right. This is data driven, so that's part of the data, alright. sqlalchemy, requests, mongoengine
1:56 whatever goes in there, Pyramid, alright. that's what goes after /project. In our route, we've defined a little variable here and this one we said
2:07 "If something like that comes into the system "map that over to details." And in addition to that, capture whatever value
2:16 goes into that {package} location and pass it into the method as, not a parameter but as a piece of data. We'll see how to get that in just a sec.
2:25 So this one's match and as soon as it finds the first match it's just like, we're done, calling this one. So it's going to prepare the request
2:31 it does a bunch of stuff. In addition to the common things it's going to go to the matchdict which is a dictionary that's always on request.
2:40 Sometimes it's empty, sometimes it's not. These variables that come from the URL the routing itself like {package}
2:46 those values get stored in that part of your request. So here our request is going to have, more than just this
2:54 but the matchdict will have at least package as a key and then that value for this URL is going to be sqlalchemy.
3:02 And it's going to call details passing that request and we can pull out that matchdict and work with it. So that's the gist with routing.
3:10 We'll see that we can do all sorts of interesting and powerful things like we could say I want to map this to details
3:17 but only if those are only letters, no numbers. Or maybe if it's only a number we want to do something different.
3:24 So later on in this section, in this chapter we're going to and do some really powerful things with routing.
3:29 But let's stop here and add this functionality to our application.

Talk Python's Mastodon Michael Kennedy's Mastodon