Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Routing for the CMS
Lecture: What is routing

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this chapter, we're going to put one of the most fundamental elements in place for our CMS.
0:06 The ability to capture an arbitrary number of different URL requests over to our website, determine through a database or something else
0:15 if we want to tell the user, "Hey, there is a page here" or not. This won't depend on the structure of our website or any given files.
0:23 It's just going to be a place to catch all the extra stuff that doesn't already get addressed and map somewhere.
0:29 And then we can decide do we want to show them a page maybe redirect them somewhere or just tell them that "page doesn't exist".
0:36 And the core concept for that is routing. So let's review, super quickly, what routing is.
0:43 On the left here we have the blue web browser. This is representing the user, visiting your web site.
0:48 On the right, we have this grey box. This is our server, and it has two Python files, which define the behaviors for packages
0:53 and for the home, just sort of the general top level stuff. In packages you can go to 'index', which is a list of all the packages
1:00 or you can specify given package and get its details.
1:04 And home, you just get a '/' that goes to the index and '/about' or something like that would maybe go there. We could decide what the URL is.
1:13 So with this set up The user is going to make a request to our server. We're going to do a get request for '/project/sqlalchemy'
1:20 and within our Web application, we've specified many different routes.
1:25 And what's going to happen with the server is it's going to go through and it's going to say, Does this match? Does this match? Does this match?
1:34 So it's just going to go through its list and say, "Well, we have a route that says for '/' show 'home/index' does, '/project/sqlalchemy' match that?
1:40 No. Well, let's see what else there is. Oh, we have this one '/about', and that's going to go to the 'home/about' behavior.
1:47 No, that doesn't match this pattern either. Now we have another pattern that '/project/<some variable piece of data> called package.
1:54 Does that match '/project/sqlalchemy'? Why, yes, It does. So what we're gonna do is we're gonna say the value of 'package' is 'SQLAlchemy'
2:04 and we're going to call the view method 'packages.details' We do that. we're gonna have pyramid set up what's called a 'MatchDict', and
2:12 in here we're gonna have the key being the variable name in the route package and the value of that key being what was passed in the URL, SQLAlchemy.
2:21 And we're gonna take that request which is set up along with the MatchDict like this and bunch of other details.
2:25 And we're just going to call that function. Well, Pyramid is going to call that function and pass the request object which
2:37 has been pre-populated like this auth. An whatever that method does. it's going to do,
2:41 You know, likely it pulls that information back from the database. It fills out some HTML and it sends that back to the user.
2:46 This is the general idea of routing, how it works in our website, and
2:50 we're going to see how we can fit into this world so that we can add our CMS routing to, catch arbitrary URLs and
2:57 then just decide that those either represent virtual pages in our website or we can say they don't represent anything


Talk Python's Mastodon Michael Kennedy's Mastodon