What Are URLs?3:28 with Kenneth Love
In Django, we control access to our views with URL patterns.
If you feel rusty or unsure about regular expressions, check out the course I did over them.
url() is a function that constructs a special object that Django uses to join URLs to view functions.
Some frameworks create implicit URL mappings. 0:00 You make a function in a certain file, you can get to that function through a URL 0:03 that's usually made up of the file name followed by the function name. 0:06 Some frameworks let you assign a route or 0:09 URL to a view when you create the function. 0:11 Django doesn't do either of these things though. 0:14 Django's URLs are created with regular expressions and 0:17 have a lot of hidden power in them. 0:19 We'll cover some of that power in this course. 0:21 For now though, let's just get a single, simple URL created. 0:23 If you got really nervous when I said regular expressions, don't worry. 0:26 Our first one's going to be super simple,and I'll link to my course on 0:29 Python regular expressions in the teacher's notes. 0:32 You probably guessed this by now, but the place to go add URLs is the in 0:35 the urls.py file in our learning_site stub. 0:40 So let's open it up and take a look at it. 0:45 Right now it has some comments at the top, and some imports. 0:47 And then there is this urlpatterns thing. 0:52 Okay, so the first thing we need to do is let's import our views. 0:58 We're gonna say from ., which means the current directory. 1:02 Import views, and it's funny, 1:05 our editor always highlights this as red like it's bad or something. 1:08 It's perfectly valid so, just a little strange. 1:14 Okay, so we can see that we have this urlpatterns, and 1:18 there's already this thing in here. 1:22 That's called a URL. 1:25 So we're gonna add a new one, all right? 1:26 And we'll put it at the bottom, cuz it's like our last thing. 1:28 If everything else fails, go to this one, right? 1:32 It's not guaranteeing that this is gonna go to this one, but just, that's the idea. 1:34 Okay, so we're gonna mark it as a raw string, cuz it's a regex. 1:38 And if we do ^$, that makes it an empty string, 1:43 cuz that's what we want. 1:49 And then we're gonna do views.hello_world, 1:52 because hello_world is what we named our view, right? 1:55 We come over here and look, hello_world, hello_world. 1:58 So, we've imported our views. 2:03 We do that so that we have access to our view functions that we've written. 2:05 And now we've added a new URL. 2:10 So now the URL objects, this thing here, actually takes five different arguments. 2:13 The first argument's the pattern, which we've given it, and 2:18 that's a regular expression. 2:22 In this case we're matching an empty string. 2:23 Starts and ends without any characters in the middle. 2:25 Any request that comes in to just your site, 2:27 with no extra path, is gonna match this pattern. 2:29 It's gonna go straight there. 2:32 Second part is the view to send the request to, or 2:33 the function, rather, to send the request to. 2:36 We want to go to the hello_world view, 2:40 the hello_world function that we created in the last video. 2:42 And we don't wanna use any parentheses here. 2:45 We're not calling the view, we just want to go to it, 2:47 we wanna know which one it is. 2:51 And the remaining parts that we're not using are keyword arguments for 2:53 the view, a name for the route, which we'll get to that later, and a prefix. 2:57 We don't need to talk about most of those, 3:01 we're only gonna talk about the name later. 3:04 Okay, so now we have a URL, we have a view. 3:06 Let's check out our homepage. 3:11 There's our Hello World. 3:13 See, that wasn't so bad, was it? 3:16 Django URLs are a great way to get more comfortable with regular expressions. 3:18 And like I said, 3:21 there's a whole other course you can take that's just about regular expressions. 3:22 Okay, I think we're ready to create our first app. 3:26
You need to sign up for Treehouse in order to download course files.Sign up