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