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 and we're actually going to start by getting a hold of this thing called an app. It's almost always named an app
0:08
you can name it whatever you want it. It's a variable, but we're going to create an instance of our Flask app like this
0:13
and this is a singleton so it gets pretty interesting sharing this across files and we'll talk about some cool patterns for doing that.
0:19
We get this single instance of an app and we're going to go and write a view method and put a decorator on there.
0:27
So we going to add the app.route decorator. This is going to be a route, talk more about that in a second but notice it defines a category
0:34
and the category's actually passed in but if there's no data from the route this is just a void method. There's no parameter's or anything like that.
0:42
We're going to write this function that accepts any arguments from the route, and nothing else really
0:46
and then in here we're going to do all of our logic. We're going to look at the URL, what this method represents
0:51
the logged in user, things like that and make a decision. Here we're doing something very simple. We're just saying, there's not a lot to show
0:58
we're just going to render out the category static HTML and we need to pass one thing to it and the name of the thing we're being passed is called key
1:08
and the value of it is just value something like that, okay. So, somewhere in the category template it's going to look at that potentially
1:16
and, you know, show that somehow in the HTML and we're just going to return that back to Flask and then we'll render that as a response to the user.
1:25
So here we're going to take these values and pass them over to this template called category dot HTML. Now that was cute, but not very realistic.
1:34
Let's take something a little more realistic here. So we're going to say, here is the method that receives
1:40
the post request for registering with account. So that means that there's an HTML page there's a whole bunch of details like
1:47
what's your name, what's your email and so on and there's a button that says register. When they click it, it's going to run this.
1:54
So there's more stuff happening here. So we're going to come in and first collect all of the data that's been submitted to us from Flask.
2:01
Here we're saying, go to the form and get the email value and the password value, but there could be other data
2:06
some from the URL, some from cookies, all sorts of stuff. Want to collect the data and then we're going to do some sort of validation.
2:13
We're going to check and see if we can create a user based on that and maybe we can because there's already an email used
2:19
right, there's already an account existing or something like that. If there is none and there's no ability to create this account
2:25
that's probably some kind of error. So we're going to return a render template. The same template, probably given before and say
2:32
There's an error, can't create your account for reasons x, y and z," but if that worked then we're going to do something else
2:37
like we're going to show them a message that says Welcome to our community, or more likely we'll redirect
2:44
them over to their account page, or some other view. Right, so this is more like what it looks like. Right, we've got a function
2:51
we get the data from forms or other locations do some processing, and then we either handle the error or we handle the proper happy path.