Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Demo: Building a CMS in 6 minutes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 For our grand finale, to do the last thing we're going to do with the routing now we're going to build a custom CMS.
0:08 Now that might sound hard, but we're actually going to do this, minus the data access layer or the editing layer, in just a couple of minutes
0:17 because routing is that powerful. So what we'll find often when we're building web applications is, there's some part of the site
0:24 that is very much driven out of the database in the sense that it has lots of structure. Like the Talk Python stuff, we have the episode
0:31 the episode has a title, it has guests, it has links it has all these sponsors, all that kind of stuff. And those pages are perfect to drive 100%
0:40 out of the database. But sometimes, you want to let people often in the marketing, create custom pages. Like I just want to have a landing page
0:49 that tells the history of our company. And maybe one that shows our team. And you don't want to go create separate views
0:55 for each of that, so often, people will fall back on to what's called a Content Management System think Wordpress, Joomla, something like that.
1:04 It turns out that with routing, we can bring that capability right into our Pyramid web app with just the smallest amount of effort.
1:14 So let's see how we do that. So you can see I've defined CMS page and a CMS controller and we're going to pass them with a page, title, and details.
1:24 Down here, we're just going to use the same structure but then we're going to show the title on the details notice we're using the structure to save
1:32 but this is trusted HTML, it's okay. So we're going to show that here. But what we need to do is allow arbitrary URLs to map here.
1:42 So the problem, well, the fact that we can't get there get it to run, 'cause we don't have the routing in place. But I want to be able to just go like
1:50 company/history shows this data. users/popular shows another. Lets say deployments/popular who's using our site, or whatever.
2:07 Just arbitrary stuff up here, and I want it to map to possible entries in the database. Basically, like Wordpress would or something.
2:16 So how do we accomplish this complicated and difficult thing? Turns out to be not bad at all. So we'll go down here and we're going to say
2:25 config.add_route like that. And then the name I said was CMS Page but what we put here is different. We're going to say that this is
2:38 whatever. Remember like in this example here this doesn't capture the / This has to be kind of basic data.
2:46 The star says, "No, no. This can be anything." So let's go over here and lets grab that. We'll say subpath = request.match. We want the Import there.
3:02 Now we'll save matchdict.get('subpath') And then let's just print out for here. We'll just put subpath for just a second
3:09 so we can see what's happening. We'll just run that real quick. So if I come over here and I say company/history. Look, we get company and history.
3:23 I'll broke them into little pieces. So let's actually convert that into, I don't know probably some kind of thing that looks like this.
3:34 Well, what I typed. We want to just get that out. So probably the easiest way we could grab the URL but then it might have query string stuff
3:42 and all sorts of weirdness, so let's just say subtext or suburl is going to be '/'.join('subpath') We'll say suburl. Now we see company/history.
4:00 Okay, you might of noticed up here you have a little fake data, so I said company/history so I'm going to request that we get these details.
4:07 They ask for employees, we'll get other details. So lets do a little bit of fake data access. So let's say page is going to be fake_db.get('suburl')
4:20 Tell PyCharm it's spelled okay for us. We'll say, if not page If they ask for something that totally didn't exist, well
4:28 we still want to return a 404, so we'll say raise HTTPNotFound from there. Otherwise, we're going to return page.
4:43 That's the page that came out of the database. It has the same structure that we're expecting. Are you ready? company/history. Look at that!
4:54 Company history, details about company history. How about employees? There's our team. Beautiful! What about other? Nope, there is no other.
5:07 Now, the big question of course is does the rest of our site still work? Yes, of course it still works. It's beautiful. Now, why does it still work?
5:19 This is super important. It works because this goes at the very end. Remember, this will match everything so it's the last chance.
5:30 So what happens is, if it gets through all of these and none of the data driven parts match it'll come down here and say
5:37 "Okay, well, why don't you give it a shot? Go to your database and see if we have some conceptual page details for that URL.
5:45 But if we don't, we're just going to let what would otherwise happen if this CMS wasn't here just a 404. But if it is, hey, let's render it!"
5:53 So this gives you a final way to sort of add on the CMS. What do you need? You need a way to make entries and edit entries in the database.
6:01 It's pretty much it. You got a CMS. The trick to make it happen is that little star right there.


Talk Python's Mastodon Michael Kennedy's Mastodon