Adding a CMS to Your Flask Web App Transcripts
Chapter: The redirects (our bit.ly)
Lecture: Listing redirects
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, let's see how we're doing with our admin over here. Now, we're working on the redirect side and let's go view the ones we already
0:08
have in the system. Yeah. No, this is not what I was hoping for. I was hoping for ah, list of existing redirects, the ability to test them, edit them,
0:18
maybe create new redirects. Yeah, so let's go work on that functionality. Right now. It turns out to be not too bad.
0:25
There's a couple of pieces we have to address. We're gonna do the H T mail. We got to do the views. We got to do the data access.
0:31
Let's do HTML first. So this one we're just gonna call redirects it is gonna be slash admin slash redirects. So, in the admin Boulder reader Rex cool,
0:47
we'll change the title. What changes to be redirects appear was just add a little way to get back. Once we're down inside this redirect lis,
0:58
we want a bill to get back to the admin section. So there's to be admin, and we'll put a little zero going back so we can do that with ampersand hash.
1:10
171 Cynical and back to Edmund home. Let's make this a button. All right, so that should have ah, hyperlink back now,
1:25
over in this area, we're gonna have a couple of things. I'm gonna start out here having an unaltered list of various items and we're gonna have
1:36
one section that lets you create new redirects will just have a hyperlink in here.
1:43
Let's let's see admin slash and redirect something like that that doesn't exist yet.
1:53
But we'll get there now. We're going to get to the more interesting part. We went for each redirect. We want to have a little block of HTML that
2:02
starts out as a list item and we wanna have, Ah, hyperlink here. That lets us test it. So we're gonna have something in here,
2:12
and then we'll just say test and let's go to make this look nice as well We'll make this bootstrap button so it would be a button.
2:21
Make it a small button, and to test it, that seems like us Easy, safe things. Let's say success like this. We're also gonna have one for editing.
2:34
So this will be admin slash and it redirect slash something and editing could change it Maybe that's dangerous. So those two are good,
2:46
and then we're gonna have a couple of spans here. Will have the name or title. Then we'll have the URL. Let's put the oil first.
2:58
Like this. Now, where are we gonna get this data from? Of course, we're going to do a loop in Jinja here. So the way that works, we're here,
3:09
and we say four are in Gonna pass a collection of redirects. We haven't done that yet. We haven't created yet.
3:15
That's next. But let's assume we're given some redirects like this. We want Teoh run that code and then in the loop there.
3:29
So for each one of these redirects were given, we're gonna create little copy a block of HTML over here. What are we gonna put? Well,
3:38
for this thing? We want to put the just the short euro. So in Jinja, the way we get string data out into a smell was we
3:48
just say double curly race or dot and then what are we gonna put? We could put the full euro or the short you're off.
3:55
We put the short one. When you click it, it'll run through the whole CMS process. So Let's say short you are l for the moment.
4:05
Now down for this one. Let's go look at our data real quick. Notice each one of our redirects has an i d. Here. So let's go and use that.
4:18
It's gonna be are not I d. Now it's good. Start out with something that's not quite right, but as seemed rights will put the short you are all here.
4:31
And then if we look at the data, there's ah, are not name. Okay, This would work if we had a view function to go to and we
4:40
could send the state into it. So the next thing, I guess. Let's go and add that view method over here. It's kind of decorators piled all over it,
4:49
but that's good. That's what we want. This will be admin reader Rex and this v slash admin slash redirects. That's cool. It's the euro.
5:02
It's easy to forget, but that index method name needs to be a reader X right. Those have to be unique.
5:10
And then over here we're gonna need a little more information than the base stuff. We're gonna also need the list of redirects.
5:15
Well, let's call this something like that Say redirect list, view, model. Now we need to go and make that and again, the easiest way to do this.
5:27
Well, over here we're gonna need our directory because we don't have an admin said of you models. Yet we're going to be plenty.
5:34
Well, let's just copy this one we already made and make this a redirect list view model and up here as well.
5:46
Okay, there's a bunch of stuff going on here that we don't need to worry about. And what we need to dio here is pretty straightforward.
5:53
Actually, we just need to have a list of the reader Rex, so you almost don't need the View model.
5:59
But remember the view model bases passing across things like the user, whether they're an admin, whether logged in,
6:05
it's better to just go with the flow, even for simple steps. So of redirects this bit is the things passed to our Jinja template. We just looped on,
6:14
and we're going to say that that is equal to what it's gonna be the CMS service and we would have Well, we have get individual redirect, get Page.
6:24
We want a all redirects yes doesn't exist does it. So let's let high charm right it over here. It's gonna have it all redirects.
6:35
And what's it gonna dio? It's going to turn a list of dicked because that's what our read Rex turned out to be.
6:44
Pretty straightforward. But if you look over here, we've got our fake data redirects, and it's almost what we want.
6:50
We want a list of that and then one of those, but not with the keys, just the values. And luckily and Python, that's pretty easy.
7:00
You just return a list of the redirects values that's it. Super easy to write. In fact, we're gonna need this for pages as well.
7:10
So let's just go ahead. And while we're sitting here, do one for pages. So now I have all pages in all redirects in just four lines of code Super easy.
7:22
So that means over here we can call this function really races just about a one in a blank line at the end there.
7:28
So it looks like this thing is ready to roll. And if we were to import it and they work, this thing might be ready to work with,
7:40
run it and see where we are okay, would click on it with her admin section fingers crossed. What's gonna happen? Wuss. We look at that.
7:52
This basically worked here. Zarb, Actor, Admin Home Super. We've got our read Rex. Here's our add new redirect. No,
8:00
just that doesn't exist yet, but we're going to write it. We can edit one and it redirect one for the edge of the other one.
8:07
Redirect to again doesn't exist yet, but it's going to we should be able to test them. So if we go toe slash courses,
8:15
notice This doesn't really look like a your l but I'll get there. We go to this one. Hey, look at that. Probably make that open a blank window,
8:23
But let's try one more by them bites. Oh, yeah, These are working so tiny. Bit of clean up over here on this section's gonna be rocking.
8:31
First of all, this one right here, let's do a target equals link that looks good. And this one is the girl, but it doesn't look like one,
8:44
so let's put a little slash here for starters. Then we go. Let's give it a class equals link like that and goto our
8:53
CSS for our admin and and at the bottom to a link. Give it a little getting type it properly not wait equals bold. That should do it. Here we go.
9:06
So now it's called courses. It goes to slash courses, and if we test it, it opens in a new page. A new tab, exactly where we were hoping it would go.
9:16
All right. So our listing of read Rex, I think it's done. Not too bad, huh? Course we got a implement.
9:23
Add and edit those turn out that basically be the same thing. But that's pretty much what's left on the redirect side of our CMS from the management