Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Routing for the CMS
Lecture: Routing examples
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's look at just a few examples that might appear in our Web application
0:05
One of the things you have to do is you have to tell it you're a bunch of files you were allowed to serve up.
0:11
Now, in our world, we have a static folder, and it has things like CSS files, images, JavaScript.
0:17
Those kind of things. Those are not determined or created by Python.
0:21
They just have to lay there and be sent back as part of the HTML that is created by Python. So what we're gonna do is I want to set up a static view.
0:29
It's going to be called static. It's going to map to the static folder, just '/static'. And then we're going to say, This is good for one hour.
0:36
The max age is specified in seconds. So this is one type of route that we might have, a static route.
0:42
More commonly, we're going to specify this more general type of route.
0:47
They all have names in pyramid. The names have to be unique because we refer to them back in other files.
0:53
We say this method handles the home route, so we just give it a unique name here and we refer to it elsewhere
0:59
We're gonna say home is going to be just '/'.
1:02
Now, you might not type '/', but this just means if you request the website without any other URL stuff, right. Like 'talkpython.fm' would map to this.
1:11
We're also gonna add one for '/help'. So we want maybe our users to build to get help using our package site. So '/help' is where they would go.
1:20
These two so far are just static. They don't have any variable data or any of flexibility in them. Right? I can type '/help', and I get this page.
1:28
If I don't type '/help', I don't get this page. I'm also not communicating information through the URL. It's just show me the content for help.
1:35
We can extend this further, like we saw for package, and we can add our URL that in the URL has some data.
1:42
So here we say the route name is 'package' and it has to be '/project/<Something something without slashes in it>'.
1:49
Like 'sqlalchemy'. Like 'pyramid'. Things like that. So we're going to say there's this route and it has to match this pattern a '/project/something'
1:58
and that something should be a variable, also called 'package'. Finally, we can also have some constraints on these routes.
2:05
So here's one. If we say we want to get the popular packages, I would like to be able to say '/7' and get the seventh most popular package on PyPI.
2:14
'/1' are the most popular one. So what we can do is we can say this is not going to match slash anything, right?
2:22
It would match '/help', potentially if it appeared before, because that's '/<something>' Just the value of 'num' would not help. That's not great.
2:31
So we can specify a custom predicate test, if you will. There's a lambda function, and it says I would like to go to the URL that's been specified.
2:40
This info of match, I'd like to get the value for 'num' whatever is passed in,
2:46
and if it's not there, just return the empty string, and I want to make sure that this is a number.
2:50
So this is our way to say only '/1' '/7' '/7000000'. Those would all match this route.
2:56
But '/help' or '/about', those won't match because 'about' is not a number.
3:03
So you can see you have a lot of flexibility? And another thing to keep in mind is when you specify these,
3:08
you want to go from the most specific to the most general.
3:13
Remember, from our example before, the way it works is pyramids gonna go through this list of specified URLs and say,
3:20
Does this match? Does this '/project' or whatever we specified? Whatever the URL is. Does it match 'home', which is '/'? No. Does it match '/help'? No.
3:31
So next one doesn't match '/products/sqlalchemy'. Yes, that's a match. And the reason you wanted to be most specific to most General is
3:39
the general ones will almost always match everything, especially in the CMS world.
3:44
You're going to see that the URL pattern is going to match literally everything.
3:48
So the stuff that has other purposes needs to appear before, or you're going to end up with just everything going to that general match.
3:56
So it has to go from most specific to most general as the tests come through.
4:00
That way, the specific ones can still catch the things that they're supposed to,
4:04
and if they don't, it's gonna fall through to the more general stuff as you go.