First App View7:38 with Kenneth Love
Let's add a view to our app to show a list of all of our courses.
include() allows you to include a list of URLs from another module. It will accept the variable name or a string with a dotted path to the default
If you don't have
include() in your
urls.py (more recent versions of Django have removed it), add it to the
import line that imports
url. That line will now look like
from django.conf.urls import url, include.
This is the Comprehensions Workshop that's mentioned in the video. Comprehensions are a great way of doing quick work with iterables.
Let's make a view in our app that will list our all of the available courses. 0:00 We'll have to do a query to select all of the courses, 0:04 but I'm sure you remember how to do that from the last video. 0:06 If you want, feel free to pause me right now and try to build the view and 0:09 URL yourself. 0:12 We've already done all of these steps, so you should be able to get most, 0:12 if not all of it, without me. 0:17 So as you probably guessed, we're going to start in courses, views.py. 0:19 Here we are in our views.py. 0:23 Django provided us with an import here at the top, 0:26 a function named render that you haven't seen before. 0:28 This is actually really, really handy for when we want to start using templates. 0:32 For this first view though, let's just return plain text like we did before. 0:35 Which means that we need to import 0:41 the HttpResponse class again. 0:45 And of course we have to create a new view. 0:51 Now I think since this view is going to show a list of courses, 0:53 I'm gonna name it course list. 0:57 And you can, of course, name it anything that you want. 1:00 And as always, it takes a request. 1:03 Now since we want to use our course model, we should import it. 1:06 So I'm gonna add that up here from .models, 1:09 which the dot means look for the models module in the current directory. 1:13 Import Course. 1:17 Take out that comment. 1:20 Okay, so course list, what do we want to do in course list? 1:22 Now I can use the model to select everything. 1:26 And then I'm gonna use that. 1:29 And I'll merge that together into a string that joins all the titles, and 1:31 I'll send that back. 1:35 Okay so we'll just have all the titles. 1:36 So let's say courses equals course.objects.all. 1:38 And then we'll say the output equals comma space dot join courses, 1:44 and then we'll return HttpResponse of output. 1:50 Okay, so we select all of the courses that exist. 1:55 We join them together with commas, and 2:01 then we return an HttpResponse that has all those names joined by commas. 2:04 Now, of course, to see this view we have to create a URL for it. 2:09 This is a little bit interesting when you want to do it for an application and 2:13 you wanna do it cleanly. 2:17 So let's take this slowly. 2:18 So first things first, of course. 2:20 We don't have a urls.py in our app, so we should create one. 2:23 So we'll say new file, urls.py. 2:26 Cool, there it is. 2:32 Okay, easy enough. 2:33 Now what goes in here? 2:35 Well, the only thing that we need to import is the URL object from Django. 2:36 And we also have to import our views. 2:42 So, lets take care of the Django import first. 2:45 From django.comf.urls import url. 2:48 And then from .import views. 2:52 Nothing special there. 2:56 We've done these two lines before. 2:57 We had both of these imports, in fact, in our site-wide urls that we made. 3:00 Back up a stage or so. 3:05 Now, if you remembered those or cheated and looked at them, then you know 3:06 that we have to create our URL patterns variable, and fill it up with our URLs. 3:11 It's kind of weird that you have to name the variable exactly URL patterns. 3:16 Python isn't usually so proscriptive, but of course, this is Django, 3:20 not just standard Python. 3:23 Django can make whatever rules that it wants, and this is a rule that it made. 3:25 Let's make our variables. 3:29 So urlpatterns equals, and it's just a list. 3:31 If you've done old Django, it didn't use be just a list. 3:34 It's so nice now that it's just a list. 3:38 So we're gonna do url. 3:40 And it's gonna start and stop, and then we're gonna save views.course_list. 3:44 I gave this the same url as our home page, is that going to matter? 3:51 Well, let's see. 3:55 Let's run the server again, manage.py runsever. 3:56 0.0.0:8000 and let's open up our preview. 4:01 Oh, I accidentally did 8080. 4:10 There we go, 8000. 4:12 So we've got Hello World! 4:14 So no it didn't matter, we still get the home page. 4:16 Now that's partly because our courses.url.py isn't 4:19 even being loaded by Django at the moment. 4:23 Django doesn't automatically look for urls.py inside of apps, and 4:25 we haven't told it to look there, so it hasn't. 4:28 Okay, let's change that. 4:32 Come back over here and let's go to our urls.py for the project. 4:34 Alright, this is our big one, right? 4:40 And we're gonna add a new line, and we're gonna put it in before this admin line. 4:42 We're gonna get to what that admin is here in a little bit, but 4:48 right now let's just add in this new line. 4:51 So we're gonna say that it starts with courses, and 4:53 we're going to include courses.urls, and then a comma. 4:58 Now, the include function here, this one right here, 5:05 lets us include a set of URL patterns from somewhere else. 5:10 In this case, we're including them from our Courses app. 5:14 But why is it in quotes? 5:18 Django turns that string that we give it, so, this is just a string. 5:19 Turns that string into an import path, which let's it find the URL patterns. 5:24 And this means that we don't have to import the URLs ourselves into every 5:29 place that we want to use them. 5:32 This really, really helps with the plugableness and 5:35 modularity that I keep bringing up. 5:37 Okay, so now, let's find out, did the home page change? 5:40 No. 5:45 What if we go to slash courses? 5:46 And we've got a problem. 5:51 We got an error. 5:52 At least that's something though right? 5:53 Since we included the course URLs at slash courses and 5:54 the only URL we have there is just an empty string, so 5:58 long as nothing comes after courses, we get that course list view. 6:00 Unfortunately though, that view has a bug. 6:04 Let's see if we can fix that. 6:06 So our problem is that we got a course at the beginning, and 6:09 it expected a string instance. 6:13 Let's go back over here and look at our view. 6:14 Now we're using join and join expects everything in here to be strings. 6:17 Well those aren't all strings, those are courses. 6:22 So we're gonna fix that with a quick little list comprehension. 6:25 So we're gonna say we're gonna join str of courses ,or of course rather, for. 6:29 For course in courses. 6:43 Then let's close our list there, okay, STR course for course in course. 6:45 If this line is a little gibberishy for you, I'd suggest checking out 6:55 the comprehensions workshop that we have here on treehouse. 6:59 I'll link to it in the teacher's notes. 7:02 Basically, what we have is a for loop inside of a list, so 7:04 that it creates a list by working through the for loop. 7:07 It's a really handy construct. 7:10 Okay, so does this fix the bug? 7:11 It did! 7:15 Check that out. 7:16 There is the list of our courses, so excellent. 7:17 Congratulations on your first Django view that uses the ORM. 7:21 That's a giant achievement! 7:24 But, as I bet you can imagine, no one wants to have to go into the Django shell 7:26 to create all of their model instances. 7:29 There has to be a better way and there is. 7:31 The Django admin, that URL we've had from the beginning, is coming up next. 7:33
You need to sign up for Treehouse in order to download course files.Sign up