1
00:00:05,210 --> 00:00:08,480
Welcome back, everyone, to this lecture on the detail view.

2
00:00:08,870 --> 00:00:15,020
The main purpose of the detailed view is to view a single instance of a particular entry inside a model.

3
00:00:15,320 --> 00:00:16,610
Let's explore how it works.

4
00:00:16,910 --> 00:00:17,990
OK, so here I am again.

5
00:00:17,990 --> 00:00:19,250
Inside vs code.

6
00:00:19,580 --> 00:00:26,990
Now, remember, previously I had a link here that allowed me to visit and list all the teachers, but

7
00:00:26,990 --> 00:00:33,410
I still remember that the actual representation of a teacher if I look at the models, will actually

8
00:00:33,410 --> 00:00:36,060
report back the subject they teach.

9
00:00:36,080 --> 00:00:41,240
But maybe this is too much information to actually display on the list of you.

10
00:00:41,570 --> 00:00:47,990
So for example, if you recall our teacher list, the HTML just says the first name and the last name,

11
00:00:48,410 --> 00:00:50,930
I could just say teacher and have that whole string.

12
00:00:51,770 --> 00:00:52,880
Go ahead and print out.

13
00:00:53,210 --> 00:00:57,920
But let's imagine there were many more fields, and I don't want to clog the list with too much information.

14
00:00:58,250 --> 00:01:05,420
In fact, what I want to do is maybe have this be a link that then goes directly to a specific teacher.

15
00:01:06,020 --> 00:01:08,470
Let's go ahead and explore that through the detail view.

16
00:01:08,900 --> 00:01:13,610
The detail for you is easy to set up and views that PI, but requires a little bit more of thinking

17
00:01:13,610 --> 00:01:16,010
and work for the template and your URLs.

18
00:01:16,010 --> 00:01:17,300
And I'll show you why in a second.

19
00:01:17,600 --> 00:01:21,590
First, let's begin by actually setting up our detailed view, and I do this up here at the top.

20
00:01:21,890 --> 00:01:31,370
I need to import comma detail view, and then let's create a instance of our own teacher detail view.

21
00:01:32,090 --> 00:01:39,050
So we'll say class teacher detail view and then we'll say Detail View.

22
00:01:40,300 --> 00:01:43,060
And we just need to connect it to the model teacher.

23
00:01:43,780 --> 00:01:45,380
And with that, we're pretty much done.

24
00:01:45,400 --> 00:01:51,940
The one thing to keep in mind is detailed views look for a specific template and the template is model

25
00:01:52,060 --> 00:01:54,460
underscore detail that each HTML schema.

26
00:01:54,850 --> 00:02:02,170
So that means in our particular case, we would create a new HTML file and I would call it teacher underscore

27
00:02:02,170 --> 00:02:03,370
detailed each HTML.

28
00:02:04,150 --> 00:02:04,690
There we go.

29
00:02:05,050 --> 00:02:09,759
And so I'll just say this is the detail view for teacher.

30
00:02:11,290 --> 00:02:16,120
And then what I passed back here is just it's really up to you what you want to display here.

31
00:02:16,360 --> 00:02:18,760
But in our case, I'm just going to say a teacher.

32
00:02:20,320 --> 00:02:20,620
OK.

33
00:02:21,910 --> 00:02:26,500
The one thing to note is that by default, you essentially use like this pure lowercase version of your

34
00:02:26,500 --> 00:02:27,010
model name.

35
00:02:27,400 --> 00:02:34,390
So if you going to use that pie, what's going to happen is this detail view sends over a context object,

36
00:02:34,390 --> 00:02:36,850
which is just the instance of a teacher.

37
00:02:37,210 --> 00:02:44,320
So what really all detail view does is hey, for a specific primary key of a teacher, go ahead and

38
00:02:44,320 --> 00:02:49,600
send them that teacher as a context object to this HTML.

39
00:02:49,870 --> 00:02:52,090
And it does this automatically just through this one attribute.

40
00:02:52,510 --> 00:02:58,000
But the key here is to figure out, well, what primary key are they actually asking for?

41
00:02:58,420 --> 00:03:02,320
Which means you can't just set up a generic detail view.

42
00:03:02,830 --> 00:03:11,020
So for example, if I tried to come to home the HTML and say, OK, go to a detail page link for the

43
00:03:11,020 --> 00:03:13,930
teacher that technically doesn't really make sense.

44
00:03:14,020 --> 00:03:14,800
And why is that?

45
00:03:15,160 --> 00:03:22,630
Well, there won't be a single generic detail generic URL path instead.

46
00:03:23,050 --> 00:03:29,150
Recall details have to do with a specific primary key of a teacher, a detailed view.

47
00:03:29,170 --> 00:03:34,700
The main idea is to return only one model entry in that case.

48
00:03:34,720 --> 00:03:40,900
Only one teacher and we know to grab a unique model entry, I need to use their primary key.

49
00:03:41,410 --> 00:03:47,200
So that means the URL actually needs to be set up in a way to specifically refer to that primary key.

50
00:03:47,770 --> 00:03:56,050
So when I connect this to my URLs, so we'll come back and go to URLs that pie.

51
00:03:56,710 --> 00:04:00,760
I'm going to import teacher detailed view.

52
00:04:01,910 --> 00:04:03,830
And then set up a path for this.

53
00:04:05,000 --> 00:04:11,780
And what I can't do is just say something like teacher detail and just leave it at that because teacher

54
00:04:11,780 --> 00:04:13,910
detail, does it actually tell me enough information?

55
00:04:14,480 --> 00:04:15,980
What specific teacher I'm looking at?

56
00:04:16,220 --> 00:04:21,500
I need to look at teacher number one, teacher, number two, teacher number three, etc. So what I

57
00:04:21,500 --> 00:04:25,760
do is, I say forward slash and then I can insert as an integer.

58
00:04:25,760 --> 00:04:33,920
The primary key will say I A. Colon peak and you pretty much have to follow this convention of peak

59
00:04:34,160 --> 00:04:37,010
because that's what the detail view is going to use automatically for us.

60
00:04:37,430 --> 00:04:39,440
And then everything else will pretty much be the same.

61
00:04:39,440 --> 00:04:48,290
We'll say teacher detail view as a view, and we can give this the name of detail teacher in case we

62
00:04:48,300 --> 00:04:49,100
want to use that later.

63
00:04:49,790 --> 00:04:50,120
OK.

64
00:04:50,600 --> 00:04:55,700
So notice here, unlike the other views that are kind of generic and you can visit them at any time

65
00:04:55,700 --> 00:05:00,920
you could create a teacher, you could list all the teachers, but you can't just go to some generic

66
00:05:01,280 --> 00:05:01,910
detail.

67
00:05:02,000 --> 00:05:04,700
You would need to know the specific teacher you're looking for.

68
00:05:05,120 --> 00:05:11,450
So we would expect these URLs to look something like this classroom teacher detail forward slash teacher,

69
00:05:11,450 --> 00:05:15,260
number one or forward slash teacher, number two, and so on.

70
00:05:15,830 --> 00:05:23,690
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

71
00:05:23,690 --> 00:05:29,360
I can pass in that primary key to this URL.

72
00:05:29,780 --> 00:05:32,180
So again, you don't do too much work.

73
00:05:32,300 --> 00:05:36,680
On the view side of things, it's essentially just one line model's equal ticker what you do.

74
00:05:36,680 --> 00:05:43,850
The work is more on the template side of things in order to successfully pass in that correct ID.

75
00:05:44,600 --> 00:05:47,030
So what we're going to do is come back.

76
00:05:48,070 --> 00:05:57,160
To our teacher list here, and what I can do is actually, instead of just listing the first name and

77
00:05:57,160 --> 00:06:01,180
last name, I could make this an anchor.

78
00:06:01,720 --> 00:06:07,060
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.

79
00:06:07,480 --> 00:06:13,210
So instead of just making this a normal list item, I'm actually going to make this an anchor tag where

80
00:06:13,210 --> 00:06:18,370
the reference will end up taking them to the teacher detail view.

81
00:06:18,850 --> 00:06:24,430
So I'm still going to have a first name and the last name, so I'm going to copy and paste those as

82
00:06:24,430 --> 00:06:28,840
the text that goes inside that actual anchor tag reference.

83
00:06:29,320 --> 00:06:30,850
So let's just do this for now.

84
00:06:31,030 --> 00:06:33,640
Save that change and see what happens to my list of you.

85
00:06:34,240 --> 00:06:35,830
So if I refresh my list view?

86
00:06:36,130 --> 00:06:37,810
Notice these are all links now.

87
00:06:38,200 --> 00:06:42,010
So I click on Adam Smith, Jose Padilla or 3rd teacher and their links.

88
00:06:42,250 --> 00:06:43,540
But these links don't go anywhere.

89
00:06:43,750 --> 00:06:44,830
Why do they not go anywhere?

90
00:06:44,830 --> 00:06:46,720
Because they don't have an actual ref?

91
00:06:47,140 --> 00:06:54,130
The question is what should the h ref be to go to this actual detail?

92
00:06:54,370 --> 00:06:56,110
Well, it should be classroom.

93
00:06:56,350 --> 00:07:02,920
Forward slash teacher detail forward slash the particular primary key for that teacher.

94
00:07:03,550 --> 00:07:07,660
So I already know I have teacher, first name and teacher last name.

95
00:07:08,020 --> 00:07:13,870
And if you were to look closely at your models, that is if you were to come to Migration's, check

96
00:07:13,870 --> 00:07:15,040
out your initial migrations.

97
00:07:15,370 --> 00:07:20,620
Remember, automatically, you get this primary key as an ID field.

98
00:07:21,460 --> 00:07:25,510
So that means I can actually grab the I.D. attribute.

99
00:07:26,140 --> 00:07:31,000
So what I do is come back to the teacher list and I set up my.

100
00:07:32,370 --> 00:07:33,150
Classroom.

101
00:07:33,630 --> 00:07:34,780
Forward slash.

102
00:07:34,800 --> 00:07:40,740
And remember, the URL says it's forward slash teacher detail and then integer peak.

103
00:07:41,070 --> 00:07:42,570
So I'm going to copy and paste that there.

104
00:07:43,500 --> 00:07:48,660
So it goes to classroom teacher detail and then the primary key.

105
00:07:49,080 --> 00:07:53,220
But I don't want the primary key for this particular teacher, so it's not a swap this out.

106
00:07:55,590 --> 00:07:56,550
And then insert.

107
00:07:57,600 --> 00:07:58,200
Teacher.

108
00:07:58,320 --> 00:07:59,490
That's it.

109
00:08:00,240 --> 00:08:04,830
And it's going to be this particular teacher at this particular point.

110
00:08:05,700 --> 00:08:07,530
So then I'm going to save that change.

111
00:08:07,890 --> 00:08:15,150
And now what I have here is links that actually go to each individual teacher's aide detail page.

112
00:08:15,690 --> 00:08:18,720
And so one thing to do is remember that in teacher detail.

113
00:08:18,930 --> 00:08:20,970
I just have a detailed view for that teacher.

114
00:08:21,720 --> 00:08:23,490
So I come back to my list of teachers.

115
00:08:24,120 --> 00:08:29,490
I refresh this and now when I click on Adam Smith, it says, here's the detail of you for that teacher.

116
00:08:29,520 --> 00:08:31,260
And now it gives me the full representation.

117
00:08:31,590 --> 00:08:33,600
Adam Smith teaches economics.

118
00:08:34,080 --> 00:08:38,159
I can now go back, go to Jose Padilla, say a teacher's Django.

119
00:08:38,159 --> 00:08:39,780
And I'm not sure what we wrote here.

120
00:08:39,870 --> 00:08:42,210
OK, this third teacher teaches math, et cetera.

121
00:08:42,690 --> 00:08:44,730
But that's how the detailed view works.

122
00:08:44,910 --> 00:08:49,200
So let me do a quick refresher of everything we just covered, because that's basically all you need

123
00:08:49,200 --> 00:08:49,530
to do.

124
00:08:50,010 --> 00:08:52,470
So step number one is set up the detailed view.

125
00:08:52,500 --> 00:08:56,250
This is the simplest that because it's just essentially a one liner connected to a model.

126
00:08:56,640 --> 00:09:01,050
Then you've got to remember that your template should be called model underscored detail the HTML.

127
00:09:01,500 --> 00:09:06,300
It's up to you what you do here, but typically you're just displaying more information about that teacher.

128
00:09:06,630 --> 00:09:11,640
You should know that the variable that's passed then is automatically equal to the model name.

129
00:09:12,450 --> 00:09:17,430
So again, because model was teacher here automatically just passed that teacher, all lowercase.

130
00:09:17,790 --> 00:09:18,090
All right.

131
00:09:18,450 --> 00:09:19,680
So that's step number one.

132
00:09:20,190 --> 00:09:26,850
Step number two is understanding the fact that for detailed views, you need to look at a singular instance

133
00:09:27,000 --> 00:09:32,130
of a teacher, which means you need some sort of unique identifier, which means you need the primary

134
00:09:32,130 --> 00:09:32,460
key.

135
00:09:32,790 --> 00:09:37,350
We already know the primary key is available to us as an I.D. attribute.

136
00:09:37,770 --> 00:09:45,930
So wherever you're trying to link to that particular teacher, then you would say here teacher ID.

137
00:09:47,070 --> 00:09:49,880
Now that's one way of working off the detail view.

138
00:09:49,890 --> 00:09:52,170
But keep in mind, you have a lot of flexibility here.

139
00:09:52,470 --> 00:09:57,990
If you really want it to, you could just create a simple form view that accepts the input of a number

140
00:09:57,990 --> 00:10:03,540
and then attempts to find that teacher number so it just forwards them to classroom for his last teacher.

141
00:10:03,540 --> 00:10:06,960
Detail Forward slash number one Number two number three number four.

142
00:10:07,380 --> 00:10:13,710
In general, I prefer this sort of methodology where I'm directly grabbing the ID off of teacher because

143
00:10:13,710 --> 00:10:18,330
it essentially doesn't let the user maybe mess up and ask for an I.D. that doesn't exist.

144
00:10:18,690 --> 00:10:23,970
I know essentially by default that if I'm able to generate this teacher from the teacher list, then

145
00:10:23,970 --> 00:10:29,730
by definition they must have an ID and I can set up that link automatically to their detailed view.

146
00:10:29,760 --> 00:10:34,770
But again, you have a lot of flexibility in our encourage you to kind of take the time to really understand

147
00:10:34,800 --> 00:10:40,170
this one line of code, because this is where essentially all the magic happens is the idea of, Oh,

148
00:10:40,170 --> 00:10:46,560
I have a teacher object, I'll just grab their I.D. and insert that as the primary key identifier for

149
00:10:46,560 --> 00:10:47,520
the detailed view.

150
00:10:47,940 --> 00:10:52,500
This is probably one of the more kind of mysterious views because it really is so simple on the view

151
00:10:52,500 --> 00:10:57,420
side of things you just connected to the model and bam, you're done all that work of having to do that

152
00:10:57,420 --> 00:10:58,980
lookup is done for you.

153
00:10:59,430 --> 00:11:02,010
But really, we can kind of understand what's happening behind the scenes.

154
00:11:02,220 --> 00:11:03,510
All detail view really does.

155
00:11:03,510 --> 00:11:10,380
Behind the scenes is it says, OK, teacher objects, and then find me the one primary key word that

156
00:11:10,380 --> 00:11:13,470
matches what's passed in into the URLs.

157
00:11:13,740 --> 00:11:15,540
But it just does this behind the scenes for you.

158
00:11:16,050 --> 00:11:16,380
OK.

159
00:11:16,860 --> 00:11:19,770
Coming up next, we're going to talk about updating a teacher.

160
00:11:20,130 --> 00:11:20,730
I'll see you there.

