Adding a CMS to Your Pyramid Web App Transcripts
Chapter: The Pages
Lecture: Adding a CMS page

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we can list the pages and we have the UI to edit and create new ones. Let's make that possible.
0:08 Again, it's gonna be basically a clone of what we did for the redirects. So I'm gonna just start by copying those and adjust
0:14 Will say edit page remember the edit template was used both for adding and editing. And down here, we're going to change this from redirect.
0:24 Anyone have a page ID. Go and just work our way through this and then name.
0:29 This would be title because pages don't have names. They have titles. So this will be page. Okay, good. Ah, the name this we can make this the title.
0:39 Title of the page. Okay, there's the title Just gonna keep going. The short url is just gonna be url cause there's no other concept for these. So url.
1:06 Like that and this last one here is not gonna be one of these. Instead, it's going to be a text area.
1:14 This is the multi-line type of input that we can have and this is gonna be contents. That's what it's called id contents as well.
1:23 Columns make this wider, say 50 rows and the value is going to be structure of contents. There we go. We want to pre populate that with this.
1:36 And actually, we want to be even a little a little more careful about how we do that. Because otherwise we have, like, spaces and stuff in there.
1:48 Like this, so exactly the contents of the page are always going to be what...
1:51 What's gonna show there, And this will be page ID create or save Page and one more bit with Paige. I think we're close, actually.
2:04 Okay, so I think this template is probably done. The other thing that has to match up with this carefully is going to be the view model.
2:11 So remember, down here we have a 'edit_redirect' view model. We're gonna have an edit page. It's gonna be similar. We have url, we have title, page ID.
2:31 Page ID So 'get_page_by' Maybe I'm gonna pass over. Remember that allows us to maybe change the url as we're going and so on.
2:46 So we're going to get it back by this ID that we're going to put in there. And there's no errors. Finally, we just need to adjust this little bit.
3:01 This will be title. I also need contents. Here we go. All right. Super. And then we're also gonna have to reverse this down here like so.
3:17 We'll test if they're trying to create a new page when one already exists without url. We'll, say "a page with that url already exists."
3:27 Woo! finally, if there is a page, but it doesn't exist by that ID you have to create it, not edit it. Almost there. We haven't written this have we.
3:42 So let's go ahead and write that function. Where's our 'get_redirect_by_id'. It's going to be basically the same,
3:49 So page ID, pages, page ID. Make that a page, and I think we'll be set. We're not going to call this "r" though. That's kind of wrong. Call that page.
4:04 Here we go. All right. I think this may actually work. Ah, one more thing, though.
4:13 One more level of putting the pieces together. That's how the Web works. It's got all these different elements.
4:18 So let's go and do an add page and add redirect. I'll put them both... I'll put them near the top. Let's give him some room. I can work on them here.
4:30 Add page. Page. I'm just going to basically replace redirect with page. Alright, So that does this one.
4:48 Now I got to do the post side of things We're going to have an add or edit page. Here we go and let's take away this edit part for just a minute.
5:05 I'm going to work on this Just like before. We're gonna have an exact copy. So let's just do this is to be edit page view model and let's see page ID.
5:24 You an update page contents and same thing here. Let's do at page and we'll go back to the pages.
5:44 Well, that's a lot. Ah, lot of stuff, right. But it's just same basic template, but obviously not going to be the same HTML.
5:52 So we're gonna need to change it. Alright? So update page, we're now we're gonna say page equals get page by ID page ID.
6:06 We should probably raise an exception.
6:07 What were we doing up here. Yeah, we're just returning as well. So we could do better error handling, and maybe we will.
6:12 But now it's going to set the various value. So age page, title, url, contents,
6:28 and we probably want to clean this up a little bit as well. We'll say 'for not url'. Something like that. Then we could say 'url = url.lower().strip()'
6:40 Remember, don't worry about casing. If contents == content.strip() we don't want to lowercase this one. This is the real typing contents.
6:51 But we don't want to have, like, spaces and stuff at the end. All right, I think that's a pretty good And let's do the same for title.
7:03 You might want to require title, but nonetheless. We can put it like this and yeah, that's a good start that lets us update the page
7:10 and then create. Let's go and just see the create over here.
7:31 I want to create with a title, url, and contents.
7:35 Still we're gonna put the I'll just put a random number here, Random. Rand-int didn't really matter what this is, but it needs the number, right?
7:44 So url, title. It's gonna be title, contents, contents, And there we have it. We're creating pages We're updating pages.
8:01 Kind of flew through that without too much summarizing what I'm doing and what not because it really is just the same.
8:04 But I didn't want to just say, "Oh, I copied everything."" So I wanted to have you see it created. So hopefully that was a good balance for you.
8:10 We're gonna see if it's working now, aren't we. We go to our admin. Go to our pages. And this one should work.
8:16 This one? Not yet. We got a tiny, tiny bit more for that one. Ah, content? Not yet. So what is the challenge there?
8:25 Over at our edit page view model. We never set 'self.contents = None'. We're setting it there. But only in the case it was Page.
8:35 Right. So let's try again. One more time. Oh, yeah. Pretty good. Pretty good. And this one, you don't really want it to be. 'None' for contents, do we?
8:45 I guess we want to put empty ''. We probably empty for all of these. One more time. Here we go.
8:53 Let's put instead a placeholder for this one, and then we'll be in good shape. There you go. That's the contents of our page.
9:09 And we should be able to create one. Let's go and just do a real simple on this will be simple test.
9:20 All right, if everything is hanging together, this may work. No! Ah, my random int it made a long ways, but the random int.
9:30 We got to put 5 to one thousand. Dumb. Let's try to submit it again then. Tada, check it out. Now a simple test if we visit it.
9:42 Yeah, that's Ah, simple test. I don't think we got the contents back did we. So really, really quickly.
9:48 We should check that we're doing that. Edit page. Nope. We sure are not. All right, now, I think we're gonna be in business.
9:59 We could say you can't create a page without having contents, but let's just create one more. Test 2 Alright let's give it a shot. Test 2. View it.
10:15 Yes, There it is. Looks like our page is working perfectly.

Talk Python's Mastodon Michael Kennedy's Mastodon