Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Since our Courses have Steps, let's add a view so we can see the steps for each course.
{% for step in course.step_set.all %}
Notice that we don't use the ()
on all()
. You don't call functions in Django's template tags, the template engine will handle that for you.
Also, step_set
is automatically generated from the foreign key. Handy!
Model.get(attribute=value)
lets you get a single Model instance by a given attribute's value.
Here is more info on prefetch_related
and select_related
. Don't bother too much with these until you're comfortable with Django's ORM.
-
0:00
Let's take a look at what all we've done so far.
-
0:01
We have a project with an app, we have a global project wide view, our homepage,
-
0:06
that renders a template.
-
0:07
That template also uses a master layout template.
-
0:11
In our app we have two models and two migrations.
-
0:14
We have our models registered with Django's admin and
-
0:17
we have a view to see all of the courses that we've created.
-
0:19
We've come a long way.
-
0:21
Great job.
-
0:22
Now let's go see about showing the details of a single course.
-
0:26
So now we have two models and our admin is set up to edit both of them pretty nicely.
-
0:31
So let's see about adding a view for looking at a single course as a student.
-
0:36
Let's get over here and let's look at views.
-
0:38
We have a way to view all of the courses.
-
0:43
We get all the course names, right?
-
0:45
It'd be really cool.
-
0:47
Let's open this up, we're going to get another.
-
0:53
All right, so we have our admin, we have our view, we have our editor.
-
0:58
A lot of tabs for editing Django sometimes.
-
1:00
So, anyway, it'd be nice if we could just click on, like, a course name, like, Boop!
-
1:05
Click on Python Basics, and go see all the steps that are in that course.
-
1:10
Normally we call this kind of view,
-
1:12
when where you're drilling into just a single item, a detail view.
-
1:15
Because you're viewing the details of a particular item.
-
1:19
So we've got views.pi open, and let's create this new view.
-
1:27
We don't have to import anything.
-
1:28
Because we've already got the render.
-
1:37
We've already have our course model.
-
1:38
We've already got that stuff.
-
1:40
Okay.
-
1:40
We do need to catch the Primary Key which is the ID field by default though.
-
1:45
And, I know, we didn't define one.
-
1:47
You remember talking about that a while ago.
-
1:49
Django automatically creates one for us.
-
1:51
So we're cool.
-
1:52
We're good on that.
-
1:53
Django saved us time.
-
1:54
Thanks a lot Django.
-
1:55
So let's say course_detail, and it of course has to take the request.
-
2:00
And then we'll say that it's going to take a PK as well.
-
2:03
Now this tells Python that we're going to have two arguments to our view.
-
2:08
Django's going to automatically supply the request when it calls the view function,
-
2:12
and we will provide the PK through our URL.
-
2:16
But we'll worry about that in a minute,
-
2:18
the rest of our view is pretty straightforward.
-
2:21
We need to get the course with the given PK and
-
2:22
then render the template, so let's do that.
-
2:24
Course equals course.objects.get, where PK is equal to PK and
-
2:30
then we're going to return render the request object.
-
2:36
Courses slash course detail, that HTML Course is going to equal course.
-
2:43
Okay.
-
2:44
Now we didn't define a PK attribute either.
-
2:48
So where did PK come from?
-
2:49
Well, Django knows what field is our primary
-
2:55
key which is what PK stands for, Primary Key.
-
2:58
By default, it's the ID.
-
3:00
So, Django's like, oh, cool.
-
3:01
You want the primary key, you want the ID?
-
3:03
Great, here you go.
-
3:04
Let's just go to it.
-
3:06
So we'll deal with what happens if the course doesn't exist,
-
3:09
a little bit later on.
-
3:10
For now, though, let's just pretend that nobody's ever going to type in a bad URL.
-
3:14
[LAUGH] And that reminds me, we need a URL for this.
-
3:17
So let's go check out our URLs.
-
3:21
So we need to add a new line for the next URL.
-
3:25
And this URL is going to be really, really similar.
-
3:28
So we're going to do r and a quote and then we're going to catch a PK,
-
3:34
with a cap P there, and then we're going to do a slash d.
-
3:40
And there's going to be one or more of those, and that's going to end the line.
-
3:45
Now it's going to go to views.course_detail and that's it.
-
3:50
So what does this do?
-
3:51
This is a named group right here, and we're going to call this group PK,
-
3:55
that's the name of the group, is PK.
-
3:58
And it's going to be one or more digits that come in the URL.
-
4:03
That's going to go to course detail.
-
4:05
All right.
-
4:07
But, notice that we left off the chevron.
-
4:11
That's because our other URLs, where are they, right here.
-
4:15
We have this chevron here.
-
4:16
So the string starts with courses.
-
4:19
It can't start with the vk.
-
4:20
Just a little bit of logic there, I guess.
-
4:23
All right, [LAUGH] so last piece, we need a template.
-
4:25
So let's get our template.
-
4:27
So inside of our templates directory, inside of our courses directory,
-
4:30
we're going to create new file and we're going to call it course detail.html and
-
4:36
inside course detail.html we want to extend
-
4:41
our layout.html.
-
4:46
And then we want to be able to set some stuff.
-
4:47
So let's set block title is equal to whatever course we're looking at.
-
4:53
That course is title.
-
4:58
All right, and then we're going to have block content.
-
5:04
So inside block content,
-
5:06
we're going to do any little bit of extra HTML just to make our designers happy.
-
5:12
So, we're going to have an article tag, we're going to have an H2 tag,
-
5:16
with the course title.
-
5:22
And, then let's do the course description.
-
5:26
And then,
-
5:30
we should probably also print out the name for each step in our course.
-
5:34
Now we can do that by looping through a new.
-
5:38
Let's add a couple of things here first.
-
5:40
So we're going to do a section so
-
5:43
we can loop through a special new attribute.
-
5:49
That's called Stepset, and
-
5:54
Stepset is a query set.
-
5:57
So it's a bunch of records.
-
5:59
And, since it's a query set, we can query against it.
-
6:04
So we're going to use all to get all of them.
-
6:07
And notice we don't include the parenthesis when we're inside of
-
6:10
Django's templates.
-
6:12
So inside here we have each thing called step.
-
6:14
And we're going to print out something.
-
6:19
So let's do step.title and
-
6:23
then let's do step.description.
-
6:29
So, we have our article tag.
-
6:31
We have an H2, we have a description.
-
6:33
We have our section tag.
-
6:35
Inside here, we have an H3 and a description.
-
6:37
We end our section, we end our article.
-
6:40
Pretty simple HTML, yeah?
-
6:42
So let's see how it renders.
-
6:44
So, we can't get to it from here.
-
6:49
But let's go.
-
6:49
We know that Python Basics is number one.
-
6:52
And we forgot to close something.
-
6:55
We did. This should be endfor not endblock.
-
6:59
Endfor.
-
7:02
Refresh.
-
7:03
There we go.
-
7:05
Python Basics is the title of our course.
-
7:07
Learn the basics of Python.
-
7:08
And then What's the deal with strings is one of the steps, and
-
7:10
there's the description of that step.
-
7:13
Not too bad.
-
7:14
Django's ORM really helped us out there with step set attribute.
-
7:18
Sometimes though that ends up being a less than optimal addition to your queries.
-
7:22
I'll link to a couple of really handy RM functions named select related and
-
7:26
prefectrulated.
-
7:27
They're outside of the scope of this course, but they can come in really handy.
-
7:30
We'll definitely cover them in a later course, but check them out right now.
You need to sign up for Treehouse in order to download course files.
Sign up