Step Detail View5:52 with Kenneth Love
We need to be able to see the details of each Step, so let's make a view just for that.
linebreaks is a filter that turns two returns in a row into HTML paragraph tags.
You apply filters to a variable with the pipe (
[MUSIC] 0:00 Great job on everything that we've done up to this point. 0:04 I think we're only missing one view. 0:07 We don't currently have any way to actually look at a single step of our 0:10 training materials. 0:13 You could probably do this by yourself by now, but 0:14 I'm going to walk through it with you one more time. 0:17 So, like we did with the course detail, we should make a step detail. 0:20 Now I would say that this view should get the PK for the course, and then the PK for 0:25 the step. 0:30 I would suggest that we'd use the order number, so we can go like step one, 0:30 step two, step three. 0:33 But, our order numbers aren't unique, so that would present a bit of a challenge. 0:34 We should find a way to fix that before we release the actual project, huh? 0:39 But we're not gonna do it in the videos. 0:43 If you wanna do that on your own, feel free. 0:44 So, let's see about making this view. 0:46 So we need to import Step, of course. 0:50 And then we need to make our new view. 0:54 So let's call this step_detail, and it'll take a request. 0:56 And then we'll say it's gonna get a course_pk and a step_pk. 1:01 Let's do step = get_object_or_404. 1:07 If we got it, we might as well use it. 1:12 Step, and we're gonna say that the course_id=course_pk and 1:15 that the pk=step_pk. 1:21 Now what is this course_id? 1:24 Well course_id is actually the foreign key field. 1:27 So what it holds is the id of the course that it goes to, and 1:31 the the field knows which table it points to. 1:36 So if we have the table, which is the course table, and 1:39 we have the ID, which is 2 or 8 or 4,010. 1:42 Then, we can figure out exactly which record to go to. 1:47 Okay, and then we need to return render, and we render the request, 1:50 of course, courses/step_detail.html, and 1:55 then the step will be the step that we got out of our get_object, or 404. 2:01 So, every good view needs a URL. 2:08 So let's see about writing one. 2:12 And we're gonna go here to urls.py, 2:13 and I think we can pretty easily start this with just a copy and paste. 2:17 So let's copy that, paste this in. 2:21 And, we're gonna call this course_pk. 2:24 And, we actually need this bit all over again, 2:28 so we'll paste that in, and we'll call this step_pk. 2:33 And this is going to go to step_detail. 2:39 Now one thing that's a little bit weird, sometimes, 2:42 is we wannna make sure and put our course_pk, or 2:46 sorry our step_pk, our step_detail before our course_detail. 2:50 And the reason why is because we wanna be sure and capture both of these things. 2:55 We don't want it to be satisfied with one of them. 2:59 So just to be safe, if you got two, cool, do that. 3:02 If it's only got one, cool. 3:05 Do that. 3:06 Sometimes they don't catch, 3:07 they fall through in a way that you don't expect them to. 3:09 So now, we have our URLs, we need to get our template handled. 3:12 So in our template directory, New File, step_detail.html. 3:17 And let's see what this is supposed to look like. 3:23 So we want to extend our layout.html again. 3:26 And in block title, let's print out the step.title and then a hyphen and 3:32 then the step.course.title. 3:41 And then we'll end our block. 3:46 So much like we can do course.stepset, which is hard to say, 3:49 we can do step.course to go the other direction in that relationship. 3:55 And then we'll do block content. 4:00 And inside of here, let's end that block. 4:04 Let's add in a bit of HTML to, again, make our designers happy. 4:08 So we have article h2. 4:13 Let's do step.course.title. 4:16 And then let's do an h3 with step.title. 4:21 That way we get the course name, and then we get the step name, and 4:26 then let's just do step.content. 4:29 And then we're going to do this pipe, and then the word linebreaks. 4:34 So what is this? 4:41 What is this linebreaks thing? 4:42 Well, the linebreaks thing is actually a django template filter. 4:44 Filters are kind of like template tags, but 4:49 they act on a bit of data that you give to them, instead of encapsulating stuff. 4:51 And in this example we're using the linebreaks filter, 4:57 which will turn two returns when you press enter twice and there's a blank line 5:00 between two paragraphs, into the closing and opening paragraph tags. 5:05 So it'll wrap our paragraphs in paragraph tags, as it should for HTML. 5:09 The data that we're giving it is the step.content attribute and 5:14 that doesn't exist yet. 5:18 We're gonna have to add that later, but you know what? 5:20 For now, let's just see if it matters at all. 5:22 So let's go back to Course 1 and Step 1 and we've got Python Basics. 5:27 What's the deal with strings? 5:33 Let's go to 2, Using the Shell. 5:36 All right, so they're there. 5:39 We still get our titles. 5:41 Everything's cool. 5:42 That's a great view, URL, and template. 5:44 But we're not done yet. 5:46 All right, let's add that model attribute. 5:48 Do you remember what else we have to do? 5:50
You need to sign up for Treehouse in order to download course files.Sign up