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