Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Before and After Requests4:27 with Kenneth Love
We often need to do things at the beginning of a request or at the end of one, before the response is sent back. In our case now, we need to connect to the database on the way in, and disconnect from it on the way out. Luckily, Flask provides two handy decorators for this.
g- A global object that Flask uses for passing information between views and modules.
before_request- A decorator to mark a function as running before the request hits a view.
after_request- A decorator to mark a function as running before the response is returned.
[MUSIC] 0:00 In this video, we're going to set up the database of our Flask app. 0:04 As part of that, we need to handle connecting to and 0:08 disconnecting from our database responsibly. 0:10 Flask gives us two really useful decorators that we can 0:13 use to make code happen as a request is coming in and as the request is going out. 0:16 Flask also gives us the super handy G object, 0:21 which lets us make different things global. 0:23 We'll use it to make our database connection and 0:26 our current user available everywhere. 0:28 Alright. So, 0:31 if we're gonna do this, we need to create our Flask app. 0:31 So, let's make a new file here. 0:36 Let's call this we'll call it app.py. 0:38 So we're gonna need to import a few things. 0:41 So let's say from flask import Flask, and I know I'm going to need g, 0:44 which we'll get to in a minute, it's this global thing. 0:49 And then we're gonna need models. 0:53 And since we're gonna have more models later on, 0:55 cuz we have to do the whole social thing and posts and all that. 0:57 I'm just gonna import models, as a whole, 1:00 instead of doing like, from models import user. 1:02 Okay. Let's set a couple of things up here. 1:05 So, we'll set DEBUG = True, we'll set PORT 1:08 = 8000 and we'll set HOST = all zeros. 1:13 If you did the previous Flask course we just always specified this 1:17 in the app.run down at the bottom. 1:20 I think it's a little bit cleaner to do these as variables. 1:22 And then we just change them in one spot. 1:25 Or if you start deploying to things like Heroku and 1:26 you're gonna use the environment variables. 1:30 Now, you can just set them up here instead of having to 1:32 kinda wiggle them around and move them. 1:35 Let's create our app. 1:37 Which remember we do Flask(__name__) and 1:39 then the two methods that we need to create 1:41 the one that runs before the request and the one that runs after the request. 1:45 Let's go ahead and create those. 1:49 So, we're gonna decorate them with @app.before_request and 1:52 then we're gonna name it, 1:56 I'm just gonna name it before_request cuz it really doesn't matter. 1:58 And so to describe what we're gonna do here, 2:03 we're gonna Connect to the database before each request. 2:06 Okay, easy enough. 2:11 And now we're gonna use g. 2:11 G is this global object. 2:14 It gets passed around all the time inside Flasks, so 2:15 we can use this to like, set up things that we want to have available everywhere. 2:18 Which is, exactly what we're going to do. 2:22 So [LAUGH], g.db is going to be =models.database. 2:25 That way it's just easier, we can always use g.db for doing stuff. 2:30 And we're gonna make sure to call g.db.connect 2:35 beginning of every single request. 2:39 Okay. 2:41 Our second one is @app.after_request. 2:41 Very creatively named. 2:48 [BLANK_AUDIO] 2:49 And so we're gonna call this one after_request as well. 2:51 And this one actually takes the response object. 2:55 So, you know, we do like return render template or 2:58 return redirect or whatever, that return there is the response. 3:01 That actually gets passed into this function, so 3:06 we're gonna describe this one too. 3:09 We'll say Close the database connection after each request, 3:10 just to make sure we know what's going on. 3:17 And we'll call it g.db.close. 3:19 And then we want to return the response. 3:23 And then if we want to, down at the bottom, let's give this, 3:26 we know we're gonna beat it in here. 3:31 We'll do our if __name__ == '__main__' and 3:33 we can call app.run(debug=DEBUG, 3:39 host=HOST, and port=PORT). 3:44 So that way, if we need to change any of those, we just change them up at the top. 3:49 We can easily turn off debug, 3:51 we can easily swap the host to something else, whatever we wanna do. 3:53 And, you know what, let's also go ahead and 3:57 just automatically insert a user or ourselves. 4:00 Well actually, lets not worry about that, we don't have the database initialized yet 4:05 so lets leave that part alone. 4:07 Having that global database will make it a lot easier for 4:10 us if we decide to build our app with a lot of different modules. 4:13 Like, maybe we'll have a views.py to hold our views instead of doing them 4:16 all in the app file. 4:19 Remember when I said that we had more to do with last login? 4:21 Let's handle that next bit in the next video. 4:24
You need to sign up for Treehouse in order to download course files.Sign up