Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: How to build a CMS in 8 minutes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I want to round out this chapter by showing you just how incredibly powerful routing can be, and I also want to dispel a myth.
0:08
Often, when people think about building web applications, it's a decision between should I use a CMS, a content management system,
0:15
something like Drupal or WordPress, where I got an editing backend and I can type in the pages, and it's just super easy for everyone.
0:23
Or should I create a data driven one like our example here. We have a lot of packages in the data base and each package gets it's own page.
0:30
Things like that. And it's often presented as an either/or. Either you have a CMS, or you have this data-driven web app.
0:38
I want to show you how we can use routing to build a CMS in under 10 minutes. I don't know how long it's going to actually take
0:45
'cause we're going to do it now live, but it won't take very long. By the time you've had the debate, the thing could be built.
0:53
That's not to say WordPress doesn't have it's place, and there's not more to do with what we're going to build here, but just to show you,
0:59
there's a lot of flexibility. So, let's get rolling. Let's go over here and add a new file called a cms_view,
1:06
views plural, I guess, to stick with the others. And then let's go over to our service and we'll have a cms_service.
1:13
So, this is going to have the various data. So, what I want to do is I'm going to define some fake data here. So, imagine I want to find two pages,
1:22
company/history, that's going to show me details about my company history, and company/employees, it's going to be our team with pictures,
1:30
but you can easily fill this out. So the way that it would really work is you would have a database, you would create these,
1:35
maybe this is the primary key and then you would fill out the details with some kind of editing form you'd have to build.
1:41
So yeah, you couldn't do it yourself in practice in 10 minutes, but it would be pretty close. So what we're going to do is we're going to use this,
1:47
I'm going to have a function called get_page, and it'll take a URL which is a string, and it's going to return let's say a dictionary.
1:58
So the way it's going to work is it's going to be pretty simple, we'll say if not your own return None,
2:03
I guess that makes the dictionary, something like that. Anyway, so we'll return that and then we'll say a page equals fake_db.geturl.
2:15
Maybe URL equals url.strip.lower, and do a little normalization here. And then we'll just return page.
2:24
And let's put an empty dictionary here as well for our default value. Either we're going to get that dictionary, or we're going to get that empty one,
2:33
so it's the same as that. Okay, so we've written this cms_service, now what we want to do is that we want to go to our
2:40
CMS view, and let me just rob that from here, something like that, so we're going to import flask, we're going to say cms, that's all good,
2:51
this'll just be CMS page, and this'll be CMS page. So of course we need to be able to display
3:01
what we get back here, so let's go and create a new folder over here, with cms, and let's just borrow the about, like so. Content here,
3:17
it's going to have a page title and a page details. So the page title goes there, and then maybe throw it into a div,
3:23
page, just copy to be sure, page details. And don't care about that. Okay, so there's the page that would display it, maybe you want a little bit more,
3:35
but it's okay, we don't need this, and then we need to go to our service, which we're going to go over here to our cms_service, and we'll get this.
3:47
We'll say CMS.get_page from the URL, what's that going to be? Now we might not get a page back, or maybe we will, so let's just return page here,
3:58
that's our dictionary, but we want to say if not page, we've not talked about this, how do you tell flask, or how do you have flask tell
4:07
the user 'Hey, this page or this thing is not here' So it's super easy, we say flask.abort and 404, page not found.
4:15
And let's return that, so we return flask.abort, and well this actually pretty much is it, except that for the routing component,
4:24
remember that's what we're focused on, so remember we were able to define stuff like, I could come over here and say URL or something like
4:32
let's call it full_url, and if I want that to be passed in, I say this, well that doesn't match things like a/b/c, it would match just like that
4:43
little element right there. We saw before that we could say int and that would make full_url and integer,
4:50
it's colored like this 'cause it's not being passed in. But we don't want it to be an integer, we want it to be a string,
4:57
we want it to be the whole path, so the way you can do that is you can say the type here is path, okay, so let's just print beginning and CMS page for,
5:10
well I think maybe that's it, we do have to register this blueprint back in our app, so let's do one of these,
5:19
and one of these, and that's ready to go, let's try running it. I think it may work, unless I've made a mistake
5:25
while I'm talking and coding and all that. So here's this, and we got our about, we got our home, and these are all working,
5:32
but what if I go to company/history? It's the moment of truth, what are we going to get? Ah, not found, not found.
5:41
Getting company/history, and the reason is, I remember we have the forward slash there so let's do this, url = '/' + url.lstrip('/'), left strip.
5:59
Okay, so in case there's a slash there, we'll just leave it, otherwise we're going to have it, so now we try one more time,
6:05
company/history, give it to me! Oh look at that, and employees. Our team, company not there, 404. 404, so if you actually look at the request that came
6:24
in on the network or HTML, 404. So not only is it give us this text, it actually is giving the right status code back.
6:34
So if we go home, home, about, perfect, we have our company, which is not specified in the CMS, quote little baby CMS, history,
6:46
like so, that went and pulled it out, so you can see, it's going for company, but then it returned 404, I typed that in wrong,
6:52
then it got this one and said yeah, that's a 200, here's your page. Look at that, look at how ridiculously simple this is.
6:59
What is that, that's like eight lines of code there, we of course have the database, right, the databases I jammed in here,
7:08
but whatever, it's not much, we had one line, or two to register the blueprint, 'cause we are not jamming it all in the one file,
7:15
and then we had one endpoint right here. So, taking it in total that's like thirty lines of code, plus a couple of lines of HTML,
7:24
and that's it to build this beautiful CMS. And we just have to build a little backend that lets people edit; like here's the title,
7:31
here's the URL, here's the content. Right, we could easily do that, grab some open source HTML editor, or better, markdown, let them enter markdown,
7:40
something like that. This shows you both the power of routing, and also if you're thinking about do I need a CMS
7:48
or do I need a data driven web app, it's not necessarily one or the other, you could have a little of both, like this.