Adding a CMS to Your Flask Web App Transcripts
Chapter: The pages (our WordPress)
Lecture: Adding the page template
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
it's time to improve how we show pages to our users. Remember our CMS request. This is the one and only view method that gets to
0:10
handle basically all the requests that show anything or respond to a user in any way
0:16
with regard to our CMS. So we've got our view model and then we say If there's a page, we're gonna return just this string.
0:23
That's not ideal. And if there's a redirect, this is pretty good. We're going to just return the redirect your l That we
0:33
made sure carries over things like the query string and what not? And if we don't have a page,
0:38
we don't have a redirect. Must be something we don't think is any sort of object in our world. So we're gonna say it's for four not found.
0:47
This is where we need to focus for now. We want to render whatever page content is in our CMS as if it belonged in
0:54
the page. It's not doing that right now. Let's see what we got. Don't go anywhere. Yes, and it just has title Is this and then just the each team l
1:07
like it. We've you know here in view source on this That does not look like Well, any other part of the site does it right.
1:16
It doesn't look like this page. It doesn't look like this page and so on. So the goal is to use a proper template that has the same shared layout and
1:27
all those features about whether or not you're logged in on this page. We're gonna focus right here for the moment.
1:35
Now, if you look at the other view methods, we have this response. Decorator, this is something we created specifically for this site.
1:43
I'll go and copy while I'm here. Let's put it up here. I'll show you what it does in a second. Let's just set up the right Uriel before I forget.
1:51
So this is gonna be in the CMS, right? Because we're in the Seimas views. There will be a CMS folder, and then let's call it something like Page.
2:02
Traditionally, we've called it the same name as we had here, but it's only really processing the situation when it is a page.
2:09
So I'm gonna be specific and say, Here's the template for wouldn't were given page, Right? So what the heck is this?
2:16
There's other ways in flask like last that render template and so on. It's fine. You can use it.
2:21
But one of the thing that's I think is much better about some other frameworks is the ability to just return a simple set of data here.
2:28
Simple dictionary. Like we could say a equals B right? We could say not equals colon. Maybe we could say title Colin pays.
2:39
I get title, etcetera. Vienna Pidge, what happens if we do this over here? Make this request. Now we get Jason response. No, not what we want.
2:56
We want to pass that data over. So that's one of the things this response template does. And it turns this dictionary into data that's passed over.
3:04
It's probably won't run if I do it this way. But if I send over the m dot get the dictionary that they will,
3:14
this will return the dictionary of all the values that are in the request. Few model. It's over here like the your l and so on.
3:24
Looks like we need to import this. There we go. All right, Now, is this gonna work? No, because we don't have one of these pages,
3:31
so Let's see if it will do anything. Nope, I guess I got to rerun it. That's not the error. Is expecting this one. Simply not found. All right,
3:42
so we got to get our CMS page in place. As usual. There's a lot of structure already here. That's it. Would like the title and the main content blocks.
3:52
And it's just so much easier to copy and paste, so we'll just call that page h t mail over there. Now, what do we want to put in?
4:02
Well, a lot to work with here already, remember were given the view model, which, if we look at it over here, these various things, you know,
4:12
I think I want to go back. I had done this and said it would be fun. Toe put this as a property. I'm gonna put this back. All right.
4:20
Well, that's all well and good. Now, over here, when we get that wouldn't have a page that's passed over
4:26
and let's go and just do the simplest thing first. Let's put the title in here. Well, say page dot Title in over into this section,
4:37
we could have our content. We could either just drop it in like this or we could put it inside a div.
4:43
Yes, I'll put inside a div page dot Going like the page is gonna have content over in our fake data pages. Right here. You see, they have content.
4:56
And here, this is the most interesting one. We have these HTML content, so we want to put those there. Let's see how this goes. We try this one,
5:05
let's try a different is not gonna come out is good. Our company employees first. Yes. Look at that. That is so much better.
5:14
We have employees. We have history, right? That looks pretty good. We've got a little bit more work to dio to make it.
5:25
Ah, little bit better as it will see, Like the donate ones Not quite gonna work.
5:29
And the more complicated that we make these pages not quite gonna work the way that we expected. But this like this,
5:35
this looks pretty legit. I feel like we're doing a good job making this feel like it looks like the rest of our site.
5:41
If we cycle between those, I mean, that's pretty awesome. The only difference here is the really the scroll bar and That's
5:47
just cause it's a bigger page one. Quick fix, though. One quick update before we move on.
5:55
Right now, we have this as plain text in those two that I showed you in the donate. When we have HTML later on,
6:03
maybe want to store this in another format as, say, mark down or something like that and then render it as HTML on the server side before we show it.
6:12
So let's make one quick change to make this more flexible. So I'm gonna say self dot HTML equals nothing.
6:19
But if there's a page, we're gonna get the contents and just put it into the HTML. It may seem silly and not really valuable at the moment,
6:37
and that's pretty much true. So I gotta call this h two males. Make sure it still works black. Refresh it. Yep. Works just exactly the same.
6:47
But this will give us the flexibility to store data in a one content and then
6:52
render it in another, which is gonna be really valuable as we switch over to things like mark down and allow you to have more flexibility there.
7:00
All right, that's it. So we've gone updated this to use a response template in the case that we return a dictionary.
7:07
It automatically maps it over to this page in the page or showing the title here and the HTML here. 01 thing I did overlook is Let's put the title of
7:18
here. How about Hi, p I this Let's just go back and see that that works appeared in si about the demo. If I refresh this FBI company history,
7:33
we got employees. I p I colon, our team. Perfect. So now we're even setting the title of the page. Not just the better of the patient.
7:43
Right? Looks pretty good. We're off to a great start on this one.