Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Demo: Adding a redirect

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have some of our CMS working here Remember, we have this super simple fake page structure that will let us look up a page by URL
0:09 and either say it's not found or return the contents Let's check that out real quick. So over here we could go to 'company/history'
0:16 Here's the company history without much formatting, or the 'company/employees'
0:22 But if we don't go to the right place. We go to somewhere that doesn't exist, This one. We get this 404 page.
0:28 So what we want to do is to be able to put in other things Like maybe we have a YouTube channel or Instagram or something like that
0:34 You could just type 'YouTube' and it'll redirect you over to our YouTube place.
0:38 Except for it's not. You can see clearly it's still are cool little 404 Not found page. That's what we're gonna do in this really quick example here.
0:47 We can get started with that. Now over and our fake data, we do have two redirects that we can pull from
0:52 Imagine they have already been created in the database. We're going to talk about storing them properly in the database shortly,
0:57 but for now, we're just gonna get him from here We have a short URL, 'courses' So it will be like going to our website '/courses',
1:05 and it's gonna redirect you over to all the Talk Python training courses
1:08 If you type '/bytes', it's gonna take you over to the Python Bytes podcast over here. Now, let's see how this works
1:18 We're gonna need to actually implement that in our CMS and this is really really simple.
1:22 So we can just go over here and say 'fake_data.redirects.get' whatever the URL is. You know, we should be a little bit careful here.
1:30 Maybe we wanna do some normalization on the URL and we probably should up there as well. So we'll say if url we'll say url == url.lower()
1:40 So in case, somebody types 'Bytes', we don't care. We want to treat the same as 'bytes'.
1:45 If they haven't have, like, a space or something, we don't care We're gonna strip off the space is gonna make it lowercase and then go ask for it.
1:53 We should probably do that up here as well. That'll make it a little more normal or canonical.
1:58 You don't have to exactly get the case right and whatnot. And then over here in our controller, well, there's some stuff going on, right?
2:05 We're generating the little sub-url from the list of elements out of our subpath URL route that we created.
2:12 So that part's already done and we're getting the pages, and it's gonna turn out to be real similar over here. We can say redirect.
2:20 And we'll just say like this. 'get_redirect' and give it the URL. However, this is not exactly what we want to do.
2:26 We got a kind of reverse this a little bit. So if we find a page, we just want to return that page.
2:32 When I go over here like so and if we get to the end, we're going to say there's nothing here, but If there's a page, we want to return that page.
2:45 We'll work on that to make it much nicer soon. If there's a redirect, we just want to send them somewhere.
2:50 So how do you do that with Pyramid? Well, we can just return, We don't need to raise this one.
2:57 But we can return http_found rather than not_found so we can import that here. Like that from pyramid exceptions. And what we put here is the URL
3:04 So we say 'redirect.get_url'. Now let's look at our data again. We're getting this thing back.
3:07 We've gotten it by its short URL. That's those things were the same, And we want to send it to where the destination is.
3:14 The name is just for us to. Keep track of and know what we're up to. All right, so try to get the page. We got it? Send the page back.
3:20 If there's no pages whatsoever, see if maybe there's a redirect for it. If there is, send them there.
3:26 If there's no pages and there's no redirect, we have no idea what they want. Tell them the page is not found. Let's give this a try
3:34 Over here we have our YouTube. That's still not going to work, which is expected. That's what we want.
3:39 Let's go over here to our help. Also not working. Let's go to one of our CMS pages, company history. Okay, good.
3:46 One of the things that should work is bytes. So this should take us to the Python bytes podcast over at Pythonbytes.fm.
3:54 Woo! Look at that! How cool. So all we had to do is go over here and type, it went away when I hit Enter.
4:01 But type r/, whatever our domain name is, 'pypi.org/bytes'. That's our little demo, that we're making at pypi.org. So we just put this on here.
4:12 We're going to go along, and it's going to find that redirect and says, Okay, well, the '/bytes' short URL.
4:18 Where is that supposed to go? That is supposed to go to this location. So we're going to return this redirect response instead of a text HTML response.
4:29 And here we go. We're over here. Perfect! Let's try the other one, courses. There's the talkPython training one. Cool.
4:36 And if we go to any other. Nope, we still just get our 404. All right, well, we've added this cool redirect feature, but like I said,
4:43 there's actually a lot more to it than just this. It's gonna be fun to build on it and make things nicer and more dynamic, we can edit them, and so on.


Talk Python's Mastodon Michael Kennedy's Mastodon