Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Jinja2 templates
Lecture: Demo: @response a better render_template
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now lets look at our views so far here we've got our index, and we've got our about I'm not a huge fan of calling flask.render_template
0:10 all of this, all the time. What I'd rather do is just say do something like this, return packages, test-test packages.
0:19 Just return some kind of dictionary that we can get from some model classes or other sorts of transformations that we want to make.
0:25 And just send those back and have it just somehow know, use index. Would be nice if that was named home
0:31 and that's named index, it would just magically find it. You know maybe we could actually write some code that'll work, and do that.
0:37 But we're going to do a little bit of a, change here. So if you go and look at the Flask documentation they
0:42 talked about different ways in which to do this. And here's one that I came up with and they talk about it about it as well.
0:48 And what I'm going to do is I'm going to use a decorator to specify, like at response. And then I'll say, you know something like, that right there.
0:59 And then we're just going to return this. Okay, so, come down here like so. I know what I want to do is I want to make a folder, that holds
1:07 a bunch of these little utility pieces and whatnot. So I'm going to call this, actually going to go up here, and run that.
1:13 Create a directory called, infrastructure. Like so, and this is some pretty touchy and not fun to write code. So let's go just copy and paste
1:27 and have a quick look at it. So here MIME type is just a variable name that Hydron thought was misspelled, it's not.
1:34 There's a little diagnostics for you. So what we're going to do is we're this decorate called, response. And it takes only keyword arguments
1:42 that means you cannot pass positional arguments. And it's a function we call that returns a decorated that just wraps any function
1:50 that takes any arguments, using itertool wraps so you get the right error error handling, error reporting and other info.
1:59 And then it's going to go in it says "Look if it's a response just return it. But if it's a dictionary
2:05 then we're going to go and actually find the template file and just do render to template, back." Also lets you set the MIME type
2:13 because that's not super easy to do as well. We're going to go over here and now if we just import this, at the top
2:21 let me see if I can have it all with that thing. Now we can say response, and this has a keyword only so if you do it like this
2:29 and let's go down here like this one. And say this one is just home-about. There's nothing interesting going on to that one.
2:36 And then we can just say "Return, empty dictionary." You may like this, you may not like this, right? It's up to you I personally like to specify
2:44 this is the page and then only return the data. You'll see in a real page like you'd have this repeated three or four times potentially.
2:51 In, uh more complicated ones. So I don't like that, not much at all. So this is the way I'm going to do it. Maybe I'll leave this one here
2:59 well at least in this chapter so you can see. So let's see if I broke it or if it still works. That's the first one to see if it's a good idea.
3:05 Hey look at that, that looks a whole lot like that homepage that looks like the about page. Pretty cool, huh?
3:12 Well I feel like this definitely simplifies this. Like I said it's again a little simple to really appreciate it
3:18 But when you have like, some conditions and some tests, you want to return you want to render that over and over but with different data like
3:25 "No you didn't submit the form right. Here's the same page but different data." So like with an error message or something.
3:31 This mode here can get super tedious. So I'm a fan of this, optional but if you like it it's going to be in the code and you can take it with you.