Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Request Variables2:58 with Kenneth Love
Our views won't always know everything. Sometimes we'll need to provide them with new arguments to get them to work like we want. One of the most common ways is through the query string part of the URL.
global: A global is a variable that exists outside of the normal Python scopes. It is available everywhere.
query string: The part of a URL that comes after the
?. You'll notice that the information after this looks like keyword arguments.
request is a Flask global that represents the request that the client has made to your application. This contains things like cookies, the path, and, in our usage, the query string.
Lots of times, we want to let users send in arguments and values through the URL. 0:00 The most common way of doing this is by using what's call the query string. 0:04 You've seen them before, they're the bit in the URL after the question mark. 0:09 In Flask, we can get to this pretty easily, let's find out how. 0:13 >> All right, so before we can get to the request object which we need in order to 0:17 get to our arguments there. 0:22 We have to actually have their request object. 0:25 Which is flask is a, it's a simple import emphasis global object. 0:28 The Python programmer in me kind of cringes at it being this global object, 0:37 but that's what it is, that's, we're gonna use it so it's fine. 0:40 This is kind of cool though it being a global object because it means the request 0:46 is available every where. 0:49 It's always available. 0:51 So that's great, but the only place that 0:52 we're really gonna deal with it is inside of our views because requests go to views. 0:56 So inside of our index view we're gonna add a couple of things. 1:01 Here in the function declaration right here, let's add a key word argument of 1:05 name and we're gonna give it, not Kenneth, let's give it treehouse. 1:10 So then what we want to do is we want to take treehouse out of here, 1:17 and we want to put something in here. 1:20 .format name. 1:22 Okay. 1:26 So, let's let's test that and see if that works. 1:27 Yeah, we still get Hello from Treehouse. 1:32 Now, you know, that, or you notice that I, 1:34 I changed this, but I didn't have to do anything down here. 1:35 I can just leave it running. 1:38 That's because we have this debug true. 1:39 That makes it reload whenever this code changes. 1:42 Pretty handy. 1:45 Okay. So 1:46 now let's make sure that we can set this new value. 1:47 In the request object there's an attribute called args that holds all of 1:51 the arguments in the request. 1:54 If you're familiar with web development this would be things like the get or 1:57 post values. 2:00 Or rather they get and post keywords and their values. 2:01 It's like a dictionary so we're gonna use the .get method. 2:04 If the name key isn't there then we're just gonna use the default name so 2:08 let's do name equals request.args.get name or just the name that was already set. 2:10 All right. 2:18 So if we get a name great. 2:19 If we don't. 2:22 Use this name. 2:23 Okay? 2:28 Okay. So, we'll refresh. 2:29 Hello from Treehouse still, and 2:31 then let's add we add question mark which is what specifies our query values, 2:33 and then we do name and we're gonna say name equals Kenneth. 2:39 And now it's named Hello from Kenneth. 2:42 So that's pretty cool. 2:44 We get a different name. 2:45 Accessing get variables is a good way to let users pass in values. 2:47 But query strings are often pretty ugly. 2:50 In fact they're so ugly I'd rather show you a better way. 2:53 Let's clean up our URLs in the next video. 2:56
You need to sign up for Treehouse in order to download course files.Sign up