Adding a CMS to Your Flask Web App Transcripts
Chapter: Routing for the CMS
Lecture: Routing examples
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's look at a couple of examples of route we could specify in flask. First off, it's good to know that there's an implicit,
0:09
static route. Most things that happen to live in the root of your website or so cannot be returned that super super important.
0:19
We have Python files there have things like possibly keys for talking to stripe,
0:26
which would be our credit card processing or A W s or things like that, or even just the logic of our application.
0:33
We don't want people to go and request like slash AP top. You try and get the contents of our site.
0:39
Luckily, they can't to. In order to get a file off the file system you have to have what's called a static route in.
0:46
Flask implicitly has one of these four slash static, So anything in the static folder like slash static slash images slash michael dot j.
0:54
Peg. They could get that, but they couldn't get something outside of this folder.
0:58
So there's an implicit route to just get everything out of the file system in slash static. So on our home page,
1:06
when you just do a request to the server to the domain name like Talk by Thunder FM. What comes back is what is mapped to the Ford Slash route.
1:15
So this doesn't communicate any information. It doesn't say, like what stuff on the home page you should show it just says, Show the default page,
1:24
whatever that means. Whatever you generate, when you run code there could randomly get different things and show it or based on
1:29
your user that's logged in and could show you stuff. The girl doesn't communicate any of that information.
1:34
We could also have another static one slash about, and this would dio and show presumably something about our product or company,
1:43
whatever. But again, static. However we saw just before that we can have routes that map data,
1:50
so slash project slash package Names of those variables go into angle brackets in flask away have angle bracket package underscored name.
2:01
That means the wool map anything that slash project slash one things so slash project slash flask slash project slash seek welcome me.
2:11
But it wouldn't map Flash project slash blast slash releases. Right. More slashes in there. It doesn't match if you just want to get one piece of data.
2:20
Here's how we pass it in the room. We create the function. The deck that we're decorating this with this round here is
2:27
going to take a variable. It's gonna have a variable called package name and flask
2:31
is gonna get this value out of the euro and pass it through automatically.
2:35
In fact, good editors like Pipe charm will warn you if there's a mismatch between the Euro variables and the function variables,
2:41
which is super super cool, we can also have constraints. What if we want to be able to say our site like Pipi i dot org's
2:49
slash seven and see the seventh most popular item? Well, if we just said slash, angle, bracket numb, well slash,
2:59
anything would match and in fact potentially about would be passed it the value for numb And it wouldn't go to its respective thing,
3:07
depending on the order in which those got registered. So that's not great. But what we can do is you can see here we
3:14
have a constraint. So int colon numb means this will match slash seven. It'll match slash 15. It'll match slash negative three,
3:24
but it will not match slash about because, you know, what about is not an integer Kuhan,
3:30
so it's super easy to have these constraints as part of these variables as well.
3:35
Now, final thing to remember is it's really important in the order in which you registered these. I have to go for a most specific to most general.
3:45
Why is that like, Let's imagine we didn't have our end constraint here.
3:49
If we put the popular thing at the top and it doesn't have the energy constraint the way flask works is gonna go. What's the first one doesn't match.
3:57
What's the second one doesn't match well, that slash numb one would match for,
4:02
say, about as you need the most specific stuff that has to exactly match. And then it can be more general as stuff falls through those cases,
4:11
and then you're going to get to him in the main take away here is when we work on our CMS route and that kind of thing that's going to go at
4:19
the very, very bottom. So we want to let our current website, with all of its capabilities, every single bit of it take a shot at matching
4:27
that pattern and doing its specific magic, so like slash Project slash Welcome here. We want to let that get handled by the package details view,
4:35
not the CMS. But if nothing else is going to catch it down at the bottom, we're going to have a look at it with our CMS in the routing
4:43
and decide if we want to give them back a page or say, you know what, no page.