Welcome back, everyone, to this lecture on the detail view.
The main purpose of the detailed view is to view a single instance of a particular entry inside a model.
Let's explore how it works.
OK, so here I am again.
Inside vs code.
Now, remember, previously I had a link here that allowed me to visit and list all the teachers, but
I still remember that the actual representation of a teacher if I look at the models, will actually
report back the subject they teach.
But maybe this is too much information to actually display on the list of you.
So for example, if you recall our teacher list, the HTML just says the first name and the last name,
I could just say teacher and have that whole string.
Go ahead and print out.
But let's imagine there were many more fields, and I don't want to clog the list with too much information.
In fact, what I want to do is maybe have this be a link that then goes directly to a specific teacher.
Let's go ahead and explore that through the detail view.
The detail for you is easy to set up and views that PI, but requires a little bit more of thinking
and work for the template and your URLs.
And I'll show you why in a second.
First, let's begin by actually setting up our detailed view, and I do this up here at the top.
I need to import comma detail view, and then let's create a instance of our own teacher detail view.
So we'll say class teacher detail view and then we'll say Detail View.
And we just need to connect it to the model teacher.
And with that, we're pretty much done.
The one thing to keep in mind is detailed views look for a specific template and the template is model
underscore detail that each HTML schema.
So that means in our particular case, we would create a new HTML file and I would call it teacher underscore
detailed each HTML.
There we go.
And so I'll just say this is the detail view for teacher.
And then what I passed back here is just it's really up to you what you want to display here.
But in our case, I'm just going to say a teacher.
OK.
The one thing to note is that by default, you essentially use like this pure lowercase version of your
model name.
So if you going to use that pie, what's going to happen is this detail view sends over a context object,
which is just the instance of a teacher.
So what really all detail view does is hey, for a specific primary key of a teacher, go ahead and
send them that teacher as a context object to this HTML.
And it does this automatically just through this one attribute.
But the key here is to figure out, well, what primary key are they actually asking for?
Which means you can't just set up a generic detail view.
So for example, if I tried to come to home the HTML and say, OK, go to a detail page link for the
teacher that technically doesn't really make sense.
And why is that?
Well, there won't be a single generic detail generic URL path instead.
Recall details have to do with a specific primary key of a teacher, a detailed view.
The main idea is to return only one model entry in that case.
Only one teacher and we know to grab a unique model entry, I need to use their primary key.
So that means the URL actually needs to be set up in a way to specifically refer to that primary key.
So when I connect this to my URLs, so we'll come back and go to URLs that pie.
I'm going to import teacher detailed view.
And then set up a path for this.
And what I can't do is just say something like teacher detail and just leave it at that because teacher
detail, does it actually tell me enough information?
What specific teacher I'm looking at?
I need to look at teacher number one, teacher, number two, teacher number three, etc. So what I
do is, I say forward slash and then I can insert as an integer.
The primary key will say I A. Colon peak and you pretty much have to follow this convention of peak
because that's what the detail view is going to use automatically for us.
And then everything else will pretty much be the same.
We'll say teacher detail view as a view, and we can give this the name of detail teacher in case we
want to use that later.
OK.
So notice here, unlike the other views that are kind of generic and you can visit them at any time
you could create a teacher, you could list all the teachers, but you can't just go to some generic
detail.
You would need to know the specific teacher you're looking for.
So we would expect these URLs to look something like this classroom teacher detail forward slash teacher,
number one or forward slash teacher, number two, and so on.
So what I'm going to do is I'm going to set up the link in a way on my template side of things so that
I can pass in that primary key to this URL.
So again, you don't do too much work.
On the view side of things, it's essentially just one line model's equal ticker what you do.
The work is more on the template side of things in order to successfully pass in that correct ID.
So what we're going to do is come back.
To our teacher list here, and what I can do is actually, instead of just listing the first name and
last name, I could make this an anchor.
So what I'm going to do here is I'm going to kind of copy and then later on, delete what's down here.
So instead of just making this a normal list item, I'm actually going to make this an anchor tag where
the reference will end up taking them to the teacher detail view.
So I'm still going to have a first name and the last name, so I'm going to copy and paste those as
the text that goes inside that actual anchor tag reference.
So let's just do this for now.
Save that change and see what happens to my list of you.
So if I refresh my list view?
Notice these are all links now.
So I click on Adam Smith, Jose Padilla or 3rd teacher and their links.
But these links don't go anywhere.
Why do they not go anywhere?
Because they don't have an actual ref?
The question is what should the h ref be to go to this actual detail?
Well, it should be classroom.
Forward slash teacher detail forward slash the particular primary key for that teacher.
So I already know I have teacher, first name and teacher last name.
And if you were to look closely at your models, that is if you were to come to Migration's, check
out your initial migrations.
Remember, automatically, you get this primary key as an ID field.
So that means I can actually grab the I.D. attribute.
So what I do is come back to the teacher list and I set up my.
Classroom.
Forward slash.
And remember, the URL says it's forward slash teacher detail and then integer peak.
So I'm going to copy and paste that there.
So it goes to classroom teacher detail and then the primary key.
But I don't want the primary key for this particular teacher, so it's not a swap this out.
And then insert.
Teacher.
That's it.
And it's going to be this particular teacher at this particular point.
So then I'm going to save that change.
And now what I have here is links that actually go to each individual teacher's aide detail page.
And so one thing to do is remember that in teacher detail.
I just have a detailed view for that teacher.
So I come back to my list of teachers.
I refresh this and now when I click on Adam Smith, it says, here's the detail of you for that teacher.
And now it gives me the full representation.
Adam Smith teaches economics.
I can now go back, go to Jose Padilla, say a teacher's Django.
And I'm not sure what we wrote here.
OK, this third teacher teaches math, et cetera.
But that's how the detailed view works.
So let me do a quick refresher of everything we just covered, because that's basically all you need
to do.
So step number one is set up the detailed view.
This is the simplest that because it's just essentially a one liner connected to a model.
Then you've got to remember that your template should be called model underscored detail the HTML.
It's up to you what you do here, but typically you're just displaying more information about that teacher.
You should know that the variable that's passed then is automatically equal to the model name.
So again, because model was teacher here automatically just passed that teacher, all lowercase.
All right.
So that's step number one.
Step number two is understanding the fact that for detailed views, you need to look at a singular instance
of a teacher, which means you need some sort of unique identifier, which means you need the primary
key.
We already know the primary key is available to us as an I.D. attribute.
So wherever you're trying to link to that particular teacher, then you would say here teacher ID.
Now that's one way of working off the detail view.
But keep in mind, you have a lot of flexibility here.
If you really want it to, you could just create a simple form view that accepts the input of a number
and then attempts to find that teacher number so it just forwards them to classroom for his last teacher.
Detail Forward slash number one Number two number three number four.
In general, I prefer this sort of methodology where I'm directly grabbing the ID off of teacher because
it essentially doesn't let the user maybe mess up and ask for an I.D. that doesn't exist.
I know essentially by default that if I'm able to generate this teacher from the teacher list, then
by definition they must have an ID and I can set up that link automatically to their detailed view.
But again, you have a lot of flexibility in our encourage you to kind of take the time to really understand
this one line of code, because this is where essentially all the magic happens is the idea of, Oh,
I have a teacher object, I'll just grab their I.D. and insert that as the primary key identifier for
the detailed view.
This is probably one of the more kind of mysterious views because it really is so simple on the view
side of things you just connected to the model and bam, you're done all that work of having to do that
lookup is done for you.
But really, we can kind of understand what's happening behind the scenes.
All detail view really does.
Behind the scenes is it says, OK, teacher objects, and then find me the one primary key word that
matches what's passed in into the URLs.
But it just does this behind the scenes for you.
OK.
Coming up next, we're going to talk about updating a teacher.
I'll see you there.