1 00:00:00,350 --> 00:00:04,160 Lots of times, we want to let users send in arguments and values through the URL. 2 00:00:04,160 --> 00:00:09,210 The most common way of doing this is by using what's call the query string. 3 00:00:09,210 --> 00:00:13,360 You've seen them before, they're the bit in the URL after the question mark. 4 00:00:13,360 --> 00:00:16,070 In Flask, we can get to this pretty easily, let's find out how. 5 00:00:17,222 --> 00:00:22,250 >> All right, so before we can get to the request object which we need in order to 6 00:00:22,250 --> 00:00:25,430 get to our arguments there. 7 00:00:25,430 --> 00:00:28,450 We have to actually have their request object. 8 00:00:28,450 --> 00:00:37,060 Which is flask is a, it's a simple import emphasis global object. 9 00:00:37,060 --> 00:00:40,950 The Python programmer in me kind of cringes at it being this global object, 10 00:00:40,950 --> 00:00:44,260 but that's what it is, that's, we're gonna use it so it's fine. 11 00:00:46,420 --> 00:00:49,620 This is kind of cool though it being a global object because it means the request 12 00:00:49,620 --> 00:00:51,200 is available every where. 13 00:00:51,200 --> 00:00:52,446 It's always available. 14 00:00:52,446 --> 00:00:56,130 So that's great, but the only place that 15 00:00:56,130 --> 00:01:01,020 we're really gonna deal with it is inside of our views because requests go to views. 16 00:01:01,020 --> 00:01:05,190 So inside of our index view we're gonna add a couple of things. 17 00:01:05,190 --> 00:01:10,388 Here in the function declaration right here, let's add a key word argument of 18 00:01:10,388 --> 00:01:15,210 name and we're gonna give it, not Kenneth, let's give it treehouse. 19 00:01:17,290 --> 00:01:19,660 So then what we want to do is we want to take treehouse out of here, 20 00:01:20,910 --> 00:01:22,500 and we want to put something in here. 21 00:01:22,500 --> 00:01:25,160 .format name. 22 00:01:26,510 --> 00:01:27,730 Okay. 23 00:01:27,730 --> 00:01:31,060 So, let's let's test that and see if that works. 24 00:01:32,500 --> 00:01:34,020 Yeah, we still get Hello from Treehouse. 25 00:01:34,020 --> 00:01:35,870 Now, you know, that, or you notice that I, 26 00:01:35,870 --> 00:01:38,480 I changed this, but I didn't have to do anything down here. 27 00:01:38,480 --> 00:01:39,960 I can just leave it running. 28 00:01:39,960 --> 00:01:42,220 That's because we have this debug true. 29 00:01:42,220 --> 00:01:45,380 That makes it reload whenever this code changes. 30 00:01:45,380 --> 00:01:45,880 Pretty handy. 31 00:01:46,910 --> 00:01:47,420 Okay. So 32 00:01:47,420 --> 00:01:51,230 now let's make sure that we can set this new value. 33 00:01:51,230 --> 00:01:54,910 In the request object there's an attribute called args that holds all of 34 00:01:54,910 --> 00:01:57,490 the arguments in the request. 35 00:01:57,490 --> 00:02:00,240 If you're familiar with web development this would be things like the get or 36 00:02:00,240 --> 00:02:01,950 post values. 37 00:02:01,950 --> 00:02:04,880 Or rather they get and post keywords and their values. 38 00:02:04,880 --> 00:02:08,180 It's like a dictionary so we're gonna use the .get method. 39 00:02:08,180 --> 00:02:10,990 If the name key isn't there then we're just gonna use the default name so 40 00:02:10,990 --> 00:02:18,770 let's do name equals request.args.get name or just the name that was already set. 41 00:02:18,770 --> 00:02:19,930 All right. 42 00:02:19,930 --> 00:02:22,490 So if we get a name great. 43 00:02:22,490 --> 00:02:23,448 If we don't. 44 00:02:23,448 --> 00:02:28,570 Use this name. 45 00:02:28,570 --> 00:02:29,090 Okay? 46 00:02:29,090 --> 00:02:30,210 Okay. So, we'll refresh. 47 00:02:31,600 --> 00:02:33,430 Hello from Treehouse still, and 48 00:02:33,430 --> 00:02:39,020 then let's add we add question mark which is what specifies our query values, 49 00:02:39,020 --> 00:02:42,780 and then we do name and we're gonna say name equals Kenneth. 50 00:02:42,780 --> 00:02:44,540 And now it's named Hello from Kenneth. 51 00:02:44,540 --> 00:02:45,850 So that's pretty cool. 52 00:02:45,850 --> 00:02:47,380 We get a different name. 53 00:02:47,380 --> 00:02:50,890 Accessing get variables is a good way to let users pass in values. 54 00:02:50,890 --> 00:02:53,520 But query strings are often pretty ugly. 55 00:02:53,520 --> 00:02:56,330 In fact they're so ugly I'd rather show you a better way. 56 00:02:56,330 --> 00:02:57,920 Let's clean up our URLs in the next video.