Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Clean URL Arguments5:36 with Kenneth Love
Query strings let us send arguments to our views, but they're often really long and kind of ugly. Your URLs shouldn't be ugly! Let's make them nicer by taking advantage of a Flask route shortcut.
Anything captured in a route argument will be a string unless you specify another type, so be careful with how you use the values. You can have multiple route arguments, too.
Vanity often gets a bad rap. 0:00 But I think it's fairly important. 0:02 Teachers want their lessons to be clear. 0:04 Designers want their products to be attractive. 0:06 And as developers, we want our code to be easy to read and work with. 0:07 We also want our URLs to be neat and tidy. 0:11 No more question mark [UNKNOWN]. 0:14 So, how do we go about getting rid of that darn question mark? 0:16 Well, let's stop using the query string. 0:20 But, I mean, that's the obvious one, right? 0:23 We get rid of the question mark, we get rid of the query string. 0:25 But we don't want to lose our ability to say hello to different people. 0:27 So, first of all, let's add another route to our view. 0:31 It's actually pretty cool that views can have more than one route, we can, 0:35 we can add more stuff in. 0:39 So we're going to do the forward slash because the routes are going to 0:40 start with a forward slash. 0:43 And then we're gonna put in this, less than name greater than. 0:44 This name tag looking thing. 0:48 So, what does that do? 0:51 Well, that means that capture, it's, it's telling Flash to capture whatever comes 0:52 after the forward slash as this value, this argument name. 0:57 So, yeah, that's pretty cool, and having those 1:03 multiple routes means that our function will respond on multiple end points, 1:06 which is pretty cool if you're building something more complex, some sort of API. 1:11 We're making something that just prints names, so it's not necessarily as wow, 1:15 we have to have this, but it's still pretty cool and we still need it. 1:20 Okay, so we still have our default value for name. 1:24 Cuz we've got one that comes in. 1:28 Right, we take this off and we still get hello from Treehouse. 1:30 But what we don't need is we don't need this anymore. 1:34 We don't need to do the name equals request.args.get. 1:37 Because the name's gonna come in through the route. 1:40 So let's take that off and since we're not using a request anymore, 1:43 let's take that off. 1:47 There's no reason to have unnecessary, crefty code. 1:48 Let's refresh and make sure it still works. 1:50 It does. 1:53 And now let's add Kenneth to the end here. 1:54 Hello from Kenneth. 1:59 Let's let's try Craig. 1:59 Hello from Craig, that's pretty cool. 2:02 But what if we want to take multiple arguments or we, I mean, we took this 2:05 name right, so, I took a name, but what if I just want to say hello from 1000? 2:10 That shows up, even though it doesn't really make sense, 2:15 1000 is not likely to be a name. 2:19 So what if we want to be able to convert these arguments to a certain type? 2:22 Well okay, saying hello is great but what I really need is a droid that 2:27 understands the binary language of wait no. 2:31 What I really need is a view that will add two numbers together for me. 2:34 Because I'm just, I'm just horrible at doing math. 2:39 I don't like doing math at all. 2:41 So, what are we going to do? 2:43 Let's add a route. 2:45 And we're going to say add, and 2:48 then we're going to say num1 and we're going to say num2. 2:49 Okay, so we got two numbers here. 2:53 And we're going to call this add and we're going to take num1 and 2:55 num2 and we're going to return this plus this equals this. 3:01 We're gonna format that with num1, num2 and num1 plus num2. 3:07 All right, let's try this, let's see if this works. 3:15 So we're gonna go to add. 3:19 And let's do two and five. 3:21 2 plus 5 equals 25. 3:25 I'm pretty sure that it's seven. 3:29 But we're using strings. 3:33 Because this came in as a string, these are, these are strings by default. 3:35 So what we need to do is we need to make these not be strings, right, so 3:39 what I can do is I can do, you know, int here, and 3:43 int here, but then I've got this really long line and 3:48 I'm having to convert the numbers that's kind of, kind of messy, so 3:51 what if instead I can just turn them into numbers, right in the URL. 3:57 Well, we can. 4:04 So this is pretty cool. 4:05 We can actually come up here and we can do int colon on both of those. 4:06 And what that says is, okay, 4:12 Flash, take this thing that comes in, turn it into an int for me. 4:13 And what's really cool is when you get something that can't be 4:17 turned into an int. 4:19 Well I tell you what, let's just let's just try that out. 4:20 And see what it does. 4:24 All right. So first of all, we refresh with our. 4:26 There we go. We got seven. 4:29 All right. When we did two and five. 4:30 What if I do a plus five? 4:31 I got a 404 because that's the string. 4:34 It can't be turned into anything else. 4:37 Right? 4:40 So the other thing to keep in mind is that what we're sending back here is a string. 4:42 Flask has to have a string that comes back out. 4:48 So if we wanted to send back just the number. 4:51 So we just wanted to send back num1 plus num2, right. 4:56 sorry, here we go. 5:02 And let's put the two back in here. 5:05 Then, see, that doesn't work, 5:07 because it gets this int object, which isn't callable. 5:09 So, what I have to do is I'd have to do, like, string of those. 5:12 And then I'd get back to seven. 5:19 But I like having the equation on hand. 5:21 I think that's, I think that's handier. 5:24 All right, now that you've seen how to create views with multiple routes and 5:26 routes that convert numbers, see if you can change our add view so 5:30 that it takes integers and floats at the same time 5:33
You need to sign up for Treehouse in order to download course files.Sign up