Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Adding redirects HTML form

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're gonna go and add our form or our HTML over here, and this one's probably good.
0:11 Here We're gonna call it for admin, I'm going to say add new redirect.
0:14 But of course, we're gonna need to change that to sometimes they add redirect, sometimes not.
0:20 Yeah, Let's actually put that inside this admin block a bit there Then what we're gonna do is we're gonna have an HTML form.
0:29 The action is just gonna be this page, but the method is going to be post west post
0:33 And in here we're gonna put standard editing elements that you'd have in a form. So when having input type equals text and I want to give an ID of name
0:42 and then it's kind of infusing a name of name what does that mean? Name is the data submitted back to the website
0:48 This is going to be the value because we need name short url, long url and so on.
0:53 So name equals name. Yeah, that's weird, but that's what it's gonna be. Class, we'll, say form control. that's from Bootstrap.
1:02 I'll give it a placeholder. Now, this is the text that's in there that says what you want to put. Say name of the redirect.
1:09 If you hover over it or use some kind of screen reader, you can get what the title is, maybe more or less the same as the placeholder.
1:17 But remember, if you pre-populated this, the placeholder won't show.
1:19 So duplicate that there and then we also want to set the value, and the value is currently going to be empty But it is what we're going to pass in now
1:26 This is important In two times
1:38 This could be the redirect that we're trying to edit later when we get to editing, but very likely if there's some kind of error like we didn't fill out this field correctly
1:40 When we want to send that information back, we don't want them to have to re type the whole form
1:44 We want a roundtrip that so they don't They keep the values they typed the first time. So we'll put that in like this.
1:51 Finally, we can say this is a required. That'll help getting that filled out there. Well, we don't do that for a few more He's one for the short URL
2:05 Here we go We have that as the URL We need one more thing, here. We also want to pass around hidden ID.
2:13 Now it turns out we could get away with this most the time, certainly for adding,
2:18 but when you're editing, especially if you're changing the short URL, you need some way to know which thing to go back to.
2:23 It gets really tricky, but down in the bottom, I'm gonna put an input 'type=hidden'. Call it, redirect-id.
2:34 We only need to show this one. I'm going to say, tau ':' condition, if there is a redirect id.
2:39 So we can omit it that if there's nothing here. Let's wrap this around. There we go. we also might have an error message
2:46 It could be that you hit save, and maybe this URL already exists, You can't create it So there's gonna be some kind of error message
2:54 We'll have a little section that will just share the outside class error message. We're only gonna show this if there is an error.
3:01 We're gonna pass that back, and the text is going to be just the text of the error.
3:05 All right, well, that's pretty good. Let's get some room to look at this big beast. So we're having our. Add a new redirect.
3:11 Here's the form submitting back to the same URL So whatever we got from GET or sending it back with the POST.
3:16 that's the get, post, redirect pattern part of it We'll have the text for the name, the short URL, the destination URL
3:24 If there's an error, we're gonna show that, and I guess we can't really do this without a button. That's going to submit our form.
3:33 Create redirect and we'll give it this class of 'button' and 'button-danger' to make it a nice red button.
3:40 All right, well, this is looking pretty good. Let's go back and look at our controller. See how we're doing.
3:44 Here's our redirect for adding add, one for get add one for post. Let's just do, we don't want this view model anymore. We need to do an edit redirect.
3:55 Now again, this is add, but the edit and the add are gonna be basically the same. We'll do that here. Right, this is going to let us edit it.
4:10 We don't need all of them, but we're gonna need some pieces of information. Whatever you saw me type here like URL, short URL, so on.
4:20 We need all of those over here. Okay, great. I think that's probably what we need. We also need error.
4:30 It could be empty. If there is no error, but it needs to be there.
4:33 The way chameleon works, the template in language, it will crash if it doesn't see these elements. So we're going to create one of these over here.
4:43 Which we gotta import and then we're gonna send back those values to that template. Woo! I think we might be ready. Let's give this a shot.
4:49 Go to our admin section. Put redirects. Add a new redirect. Oh, yeah, there it is. And it's required.
4:57 Yeah Okay. Well, that's a good start. It actually making more happened than you think.
5:02 It's doing the local validation. So it's not submitting that back.
5:06 Great, so that's working pretty well. Let's go and actually do this part as well. You can just do print.
5:17 We can just send back the request dictionary that was sent over. So the view model captures all this data from the form and from the URLs and so on.
5:24 And it'll show it there. Let's just see what's up with this. It won't let us submit until we fill it out.
5:29 So let's say we want to go to Talk Python rather than Python Bytes. So we'll just say Talk Python. We'll give that for the name there.
5:38 Fill this out. something like, We want to go to a slash just say type talk and then I'll take us there. And then, of course,
5:46 This is not gonna work, We're gonna try to create it and says. You know, we're just we would have but let's see what we got.
5:52 There's a bunch of stuff captured from various places that don't matter.
5:57 But this is the part we care about. The name is Talk Python. The short URL is talk and the URL is that. Perfect, It looks like everything is working

Talk Python's Mastodon Michael Kennedy's Mastodon