Adding a CMS to Your Flask Web App Transcripts
Chapter: Routing for the CMS
Lecture: Examining the existing routes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 so very soon we're going to go work on our CMS routes and add that capability that I've been describing over the past few videos.
0:08 But before we dio, let's take a look at the routing that is already in place because remember this website here it already does quite a bit.
0:16 We managed to get it open here. You can see this is our home page. We have ah, log in section we have a register section to go back home
0:26 go see our boat Okore and so on. Looks like something's missing about our Siham are CSS,
0:33 but never mind you get The point is we have all those existing routing that's happening
0:38 here, so let's quickly see how that works because we want to plug into the whatever technique or pattern were using their Let's go look at good again.
0:50 Now, in a nice, nicely factored, nicely organized flask application, there's two general places you need to look for the Orioles
1:00 If you follow the super simple getting started, dump everything into the APP top people I file.
1:05 Well, there's just one, but that's not the right way to do it. So, first of all, here's a copy over to chapter four of our flask
1:14 code. See, there's a code is we're starting from. We're gonna edit this and add the features of the first place to look is in
1:22 the view files. So we're using this concept called Blueprints right here were created one and blueprints let you organize parts of your views.
1:32 Part of the methods that handle your l requests in flask into different files like,
1:38 Hey, guess what? Everything that city with accounts goes in here and everything else
1:41 to do with packages goes into this file instead of cramming it all together. And we're gonna leverage these blueprints to do that.
1:48 Over here, in the home view, we say, Here's an index method and it's going to be four slash There's the
1:54 amount method it's gonna handle slash about and normally you would say apt out route like this, But notice we're not doing that.
2:03 We're seeing Blueprint that route because we want to organize things a little bit better, right? So, blueprint or app,
2:10 depending on the style of your doing, So here, this is gonna give it the name home. It knows the words templates lived.
2:17 So when you say slash home slash index It really means templates slash home slash index All right, so we're gonna go here.
2:25 Just have these two static routes. This is not super interesting. Let's go look at the account view one. So if you go and just you're logged in,
2:33 you say slash account that will show you details about when you're logged in. But if you're not, it'll send you over to slash account slash log in.
2:41 So if you want to register, have accounts last register and account slash register.
2:46 But we have either get or post Some people musch this together and like a one
2:51 giant function that handles both showing the form and handling it don't do that.
2:57 Just have these two separate methods that have these two jobs Same thing for log in account slash Logan account slash log and getting post.
3:05 And then you could just long out. These are all super simple over in packages.
3:11 This is slightly more interesting. This is where the interesting routing is anyway. But here we have our slash project slash package name.
3:19 Now I click here, notice that this part and this part is also highlighted.
3:24 If I move away see how that hi charm knows that this variable and that variable those things go together. Okay.
3:32 And if I put something out of place there, notice that this gets a warning that says the function package details does not have the
3:40 prime merger package name. Now it does, right? So it's super cool. Those things get together. But that's how we specify passing in the data from the
3:49 euro into Yeah, I'm also we have our into drink and then who were passing in our integer, which is an energy that's pretty awesome.
4:00 It automatically takes care of that force, and that's being passed over. And we're not really doing much useful or just saying
4:06 Here's the whatever most popular package. It's funny when you put the like, a two or something. But this route, this popular one will handle just that.
4:15 Mapping under s CEO. We just have a few things for, like, site map. So if you request site map dot xml, you don't really get a file.
4:23 You get that computed on the fly and again or robots a text. It just returns robots dot tax there.
4:31 Well, that's pretty much it for all the blueprints and each one of them they're defined. Appear the top called Blueprint.
4:37 Now we need to put this all together over in our main application file. Over here, there's a bunch of stuff is happening.
4:44 This is running it. And we're also and figuring it will see the bottom how that comes together, what? Configuring it here.
4:52 And then we're starting it, or you just configure it. So over here we have our logging. We have our blueprints, and we have worked database.
4:58 So the part that we care about is down here, this register blueprints. So in this section,
5:04 we're gonna go in or say Give me the home views and then I go APP register, blueprint register, blueprint,
5:11 home views blueprint. And then saying for package, we're gonna register all of its stuff all of its routes from the blueprint count.
5:21 And then this s E o thing. And so what we want to do is but our CMS routes here.
5:29 Okay, so we're agreed. Something kind of like this would have a CMS views but have a CMS blueprint. The CMS blueprints gonna have routes,
5:36 and they're gonna get registered right there. Why? Because that's at the end, Remember, Specific to general. So that's how we're gonna do it here,
5:46 and that's it. That's the way the routing works in the current application,
5:50 and then we're going to plug into that and just add our routes onto this model


Talk Python's Mastodon Michael Kennedy's Mastodon