Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Introducing the Flask framework
Lecture: Building block: Views
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Here's a view method
0:01
and we're actually going to start by
0:03
getting a hold of this thing called an app.
0:06
It's almost always named an app
0:07
you can name it whatever you want it.
0:08
It's a variable, but we're going to create
0:10
an instance of our Flask app like this
0:12
and this is a singleton so it gets pretty interesting
0:15
sharing this across files
0:16
and we'll talk about some cool patterns for doing that.
0:18
We get this single instance of an app
0:21
and we're going to go and write a view method
0:24
and put a decorator on there.
0:26
So we going to add the app.route decorator.
0:28
This is going to be a route, talk more about that in a second
0:31
but notice it defines a category
0:33
and the category's actually passed in
0:35
but if there's no data from the route
0:36
this is just a void method.
0:38
There's no parameter's or anything like that.
0:41
We're going to write this function that accepts any
0:42
arguments from the route, and nothing else really
0:45
and then in here we're going to do all of our logic.
0:48
We're going to look at the URL, what this method represents
0:50
the logged in user, things like that and make a decision.
0:54
Here we're doing something very simple.
0:55
We're just saying, there's not a lot to show
0:57
we're just going to render out the category static HTML
1:02
and we need to pass one thing to it
1:04
and the name of the thing we're being passed is called key
1:07
and the value of it is just value
1:10
something like that, okay.
1:11
So, somewhere in the category template
1:13
it's going to look at that potentially
1:15
and, you know, show that somehow in the HTML
1:18
and we're just going to return that back to Flask
1:21
and then we'll render that as a response to the user.
1:24
So here we're going to take these values and pass them over to
1:27
this template called category dot HTML.
1:31
Now that was cute, but not very realistic.
1:33
Let's take something a little more realistic here.
1:36
So we're going to say, here is the method that receives
1:39
the post request for registering with account.
1:43
So that means that there's an HTML page
1:45
there's a whole bunch of details like
1:46
what's your name, what's your email and so on
1:48
and there's a button that says register.
1:50
When they click it, it's going to run this.
1:53
So there's more stuff happening here.
1:54
So we're going to come in and first collect all of the data
1:57
that's been submitted to us from Flask.
2:00
Here we're saying, go to the form and get the email value
2:02
and the password value, but there could be other data
2:05
some from the URL, some from cookies, all sorts of stuff.
2:08
Want to collect the data
2:09
and then we're going to do some sort of validation.
2:12
We're going to check
2:13
and see if we can create a user based on that
2:15
and maybe we can because there's already an email used
2:18
right, there's already an account existing
2:20
or something like that. If there is none
2:22
and there's no ability to create this account
2:24
that's probably some kind of error.
2:26
So we're going to return a render template.
2:29
The same template, probably given before and say
2:31
There's an error, can't create your account for
2:33
reasons x, y and z," but if that worked
2:35
then we're going to do something else
2:36
like we're going to show them a message that says
2:40
Welcome to our community, or more likely we'll redirect
2:43
them over to their account page, or some other view.
2:45
Right, so this is more like what it looks like.
2:48
Right, we've got a function
2:50
we get the data from forms or other locations
2:53
do some processing, and then we either handle the error
2:57
or we handle the proper happy path.