Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Routing for the CMS
Lecture: Adding THE cms route

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Sometimes life is full of irony and this is one of them.
0:04 We've talked about all of this routing infrastructure and all of these routes that we've added.
0:09 And you think, Well, this is what's here already know how much more do we have to add and work with our CMS?
0:15 Well, it turns out we're only gonna add one single route for all of our CMS.
0:20 And that's because the way the CMS works is it says all the stuff that didn't match any of these,
0:26 just give that to me. I'm gonna take that, have a look and see if I can work with it.
0:31 So it'll basically pass through our CMS method and the method is going to decide
0:35 That URL you gave me, Does that match a page or some kind of redirection or something like that?
0:40 Or is it something that I don't know about. It's not in my database and there is no record of it.
0:45 So just go back and tell them that the page doesn't exist as if there was no CMS section.
0:50 So what we're gonna do is, we're going to come in here. We're gonna have a CMS route.
0:55 Now it's super important that this goes at the very end because, as I described it,
1:00 The CMS route, It's gonna catch every single request period except for the ones that are preceding it.
1:06 But if it is in the beginning, that means everything. So we're gonna go down here. We're gonna do this 'config.add_route()'
1:15 like so, and we can call it CMS, 'cms_request'. Let's call it a request. That's what we're going to call it.
1:21 Now, we've seen that over here we can have static, and we can also pass in variables like that.
1:28 But what we're gonna do instead is we're gonna pass in something a little bit different
1:32 and we're gonna use a star, and that just means match everything And we need to give it a name. We'll call it 'subpath'.
1:39 So an example of what subpath might look like. This might be '/home', let's do this. '/company' '/history'.
1:49 Right this could be a URL that we come up with, and when we pass it over That's gonna be the value of subpath.
1:57 We'll have to figure that out there actually think it might be.
2:01 We're gonna see I think it might come back as a set a list of 'company' and 'history' for when you ask for a '/company/history'.
2:07 We'll see in a second. I'm pretty sure that's the case.
2:13 But what we're gonna do is we're going to say I would like to put a pattern that's going to match everything.
2:19 And then we need to take this and apply it just like we had over in these other controllers Like here,
2:24 We need to apply it to one of these So just for a little quick start, I'm gonna copy that.
2:28 And let's go and create a new Python file. I'll call this CMS controller, as in model view controller pattern.
2:37 And let's go over here and say hi to the view config which we're gonna need to let PyCharm do a little import statement at the top, Thanks, PyCharm.
2:44 You don't need this. Just 'return response', which is gonna come out of pyramid And 'body="Hello CMS"'. Here we go.
2:55 So what goes here? This will be CMS request. We're not going to have a render for the minute.
3:01 And let's do it like this. We'll put this down here. Comment that out. Great.
3:05 So what we're doing is we're going to say, move over so it doesn't get greyed out on you. There we go.
3:10 So we're gonna have this route come to. Do it like that.
3:16 We're going to pass in the request, which at the moment we're not using, but we will, very soon.
3:20 And we're going to say 'Hello CMS'. We're not using any of the data that's getting past.
3:24 Let's see if this works. We might need to do one other quick change here. All right, so this is still working. All these were not taken over.
3:34 Let's say 'Hello World'. Tada, look! Now we type whatever we want. Anything up here, it doesn't matter. All of those are matching that pattern.
3:46 Now, we obviously don't want to just say 'Hello World' to anything on the Web site. That's silly, but this is the first step.
3:52 Let's go a little farther and have it take that information that's passed over, get it out and do something with it.
3:59 So we're gonna go over here and say sub path, go to the request Now, what can I do on the request? I don't know.
4:07 Looks like nothing because, Well, PyCharm's unhelpful here. So we go over here and say this is a request and specify a type from pyramid.
4:15 Ah, that's better. We go the dictionary that contains the stuff from the URL and say, Give me the subpath.
4:23 Let's just print out really quick what is the subpath and what's the type of subpath. Run that again, switch over here do a quick request.
4:32 All right, so it's a tuple. I almost had it right. I said it was a list. It's a tuple.
4:37 So these are the things that are separated by the forward slash right? It slash on the server? Then 'hello', then slash 'world' and slash anything "!".
4:48 So we probably want to get the URL and the way we'll get the URL is from this subpath.
4:55 We can come over here and say Use this cool little string function.
4:58 Say, given a string, I would like to take a bunch of items and use the string to turn them into other strings.
5:03 Stick those this item in the middle stick the slash in the middle like they join on subpath.
5:09 And that's a print out, we don't need to print out, Say 'hello world'. 'Hello CMS'
5:14 you requested and let's put your URL in here as a little f-string like so. Right. Again.
5:21 What are we giong to get now? instead of 'hello, CMS'. If we did it right, it should be requested, this.
5:27 All right, let's see. There it is! Hello, World, anything? Beautiful. Beautiful. Beautiful.
5:33 So it looks like we've got this URL mapped over to this CMS method,
5:39 and now we just look at the URL and decide. Do we have content to show them for that? Yes or no?
5:45 This is gonna be the foundation that'll hook into this data driven web application. For the most part, it doesn't go to the CMS, right?
5:52 We're over here and log in. We can register. We can go view some stuff about packages, those are, all the strict data driven, structured pages.
6:02 But then, if we want to go say '/donate' well, we could create a CMS page that talks about donations.
6:08 We want to talk about help, here's the CMS page that's gonna, you know, provide users help. Over here we have featured products. In featured/projects.
6:17 We can use our CMS to start adding features without going and recreating very structured things in the database that are, you know, not very helpful.
6:26 These are one-off pages like, Here's the featured projects. Or here's the help page or something like that. The hook to make that happen is in place.
6:35 The requests gonna come in here, we're gonna figure out. Well, what do we want to show them for this URL and then send it off? It sounds simple.
6:42 There's a lot of cool stuff that we're going to do to make this really, really nice and fast and easy to create these pages.
6:47 But this is the shim. This is the seam in our application where we can reach in,
6:52 and then we plug in our custom code that we write that will effectively be our CMS.

Talk Python's Mastodon Michael Kennedy's Mastodon