1
00:00:05,220 --> 00:00:10,140
Welcome back, everyone, and the previous lecture, we used a create view to create a new instance

2
00:00:10,140 --> 00:00:12,780
of our model in this particular case, a teacher.

3
00:00:13,290 --> 00:00:18,630
Now I did tell you you could go through the admin panel to confirm that actually worked, but we could

4
00:00:18,630 --> 00:00:24,630
also do is create a list for you, which is going to list all the instances for a particular model.

5
00:00:24,870 --> 00:00:25,620
Let's check it out.

6
00:00:26,160 --> 00:00:28,430
OK, so here I am back underneath views.

7
00:00:28,710 --> 00:00:34,410
What I'm going to do is create a new view that can list all the instances and any particular model.

8
00:00:34,830 --> 00:00:42,570
So we scroll back up here and again from jangled up views that generic I'm going to import a list view

9
00:00:43,260 --> 00:00:46,610
and then we'll scroll down here and it's going to look quite similar to the create view.

10
00:00:46,890 --> 00:00:50,520
As I mentioned earlier, these are going to start to look a little repetitive, but that's kind of the

11
00:00:50,520 --> 00:00:50,880
point.

12
00:00:50,910 --> 00:00:54,210
It should be easy, repetitive and simple to remember how to use these views.

13
00:00:54,780 --> 00:01:01,680
So we're going say class and we'll say teacher list view and we inherits from list view.

14
00:01:02,370 --> 00:01:06,450
And again, all we need to do is connect this to the model.

15
00:01:07,400 --> 00:01:08,810
That we're looking for.

16
00:01:08,930 --> 00:01:14,640
So we'll say connect it to the teacher model, and actually it could just work with that.

17
00:01:14,660 --> 00:01:16,130
So I'm going to save this for right now.

18
00:01:16,490 --> 00:01:22,400
Essentially, all this says is list every instance of a teacher now by default.

19
00:01:22,610 --> 00:01:27,200
This also has a template name that it looks for, and that template name is going to be model underscore

20
00:01:27,200 --> 00:01:31,110
lists dot each HTML in the same way we had great view.

21
00:01:31,250 --> 00:01:33,680
Look for teacher underscore form, but it's HTML.

22
00:01:34,040 --> 00:01:37,490
This is not looking for model underscore lists that each HTML.

23
00:01:37,880 --> 00:01:40,110
In fact, let's create that teacher list now.

24
00:01:40,220 --> 00:01:41,180
So I'll go to a classroom.

25
00:01:42,080 --> 00:01:46,160
Create a new file and call it teacher underscore list.

26
00:01:46,160 --> 00:01:47,060
Thought each HTML.

27
00:01:47,780 --> 00:01:50,270
And this is going to be a list of all the teachers.

28
00:01:50,450 --> 00:01:55,850
So say each one list of teachers, and I'll let you know it's a list of you.

29
00:01:56,870 --> 00:01:59,690
And then what I'm going to do here is create an ordered list.

30
00:02:00,350 --> 00:02:02,150
And this is where I can begin to use some jingo.

31
00:02:02,360 --> 00:02:05,300
So I'll say four and I'll say for teacher.

32
00:02:06,340 --> 00:02:13,640
In and then object underscore list, I'll talk more about how I know this called object list later on.

33
00:02:14,590 --> 00:02:17,500
And then what I want to do is pretty much for every teacher here.

34
00:02:17,680 --> 00:02:24,820
I'm going to create a list item and we can just say this is a teacher, and let's just use, let's say,

35
00:02:24,820 --> 00:02:25,510
their first name.

36
00:02:26,610 --> 00:02:28,950
And then we'll insert teacher.

37
00:02:30,450 --> 00:02:31,080
Last name.

38
00:02:31,890 --> 00:02:32,250
OK.

39
00:02:32,760 --> 00:02:35,970
And that's pretty much what we're going to do here now.

40
00:02:36,000 --> 00:02:40,110
Next, I need to actually connect this view to a URL, so we'll go ahead.

41
00:02:40,110 --> 00:02:46,470
And after we save that change under teacher list of you come to your URLs and imports on, I start breaking

42
00:02:46,470 --> 00:02:51,420
this up into multiple lines of parentheses, just so it's easier to see all the views I'm importing.

43
00:02:52,230 --> 00:02:54,600
So I'm going to import list.

44
00:02:54,610 --> 00:03:00,930
Actually, it's teacher list, view, teacher list, view close parentheses there to have this on multiple

45
00:03:00,930 --> 00:03:09,810
lines, and let's create that new URL path will say this goes under list underscored teacher or teacher

46
00:03:09,810 --> 00:03:11,580
lists kind of whatever you really want to call it here.

47
00:03:11,580 --> 00:03:12,630
It's just the URL path.

48
00:03:13,140 --> 00:03:15,900
And then we say teacher list of you.

49
00:03:15,900 --> 00:03:20,130
And remember for these because they're class based views and path expects a function.

50
00:03:20,130 --> 00:03:24,390
We say as view, and then I can give it a name like list teacher.

51
00:03:27,690 --> 00:03:27,980
OK.

52
00:03:28,680 --> 00:03:33,450
Save those changes, and the last thing I want to do is actually connect this to a link inside home

53
00:03:33,450 --> 00:03:34,160
that HTML.

54
00:03:34,770 --> 00:03:36,960
So I will copy what we've been doing before.

55
00:03:38,160 --> 00:03:44,280
And then create a new list item here, but this will be not great teacher, but list teacher, and then

56
00:03:44,880 --> 00:03:47,430
we'll say this is the List Teacher page link.

57
00:03:48,030 --> 00:03:53,100
So let's save those changes and come back to my original welcome page.

58
00:03:53,130 --> 00:03:55,050
Make sure we didn't commit any typos or errors.

59
00:03:55,470 --> 00:03:59,310
So now my welcome home page, I have that new link list teacher page link.

60
00:03:59,670 --> 00:04:02,400
I click on that and I see a list, the teacher list view.

61
00:04:02,790 --> 00:04:04,680
Jose Padilla the first name and the last name.

62
00:04:05,130 --> 00:04:11,790
Let's go back and go to create New Teacher Page Link and check it out just to confirm it's working.

63
00:04:11,790 --> 00:04:16,079
So I'll say Adam Smith and subject, let's say economics.

64
00:04:16,560 --> 00:04:16,910
OK.

65
00:04:17,579 --> 00:04:18,240
Hit submit.

66
00:04:18,570 --> 00:04:20,160
We're now at the Thank You page.

67
00:04:20,190 --> 00:04:27,030
And now let's go back to our home page and then what we can do here is check out the List Teacher Page

68
00:04:27,030 --> 00:04:30,180
Link and now I see Adam Smith perfect.

69
00:04:31,370 --> 00:04:38,720
Now, let's discuss a few more options for the list of you, so by default, you can see it's extremely

70
00:04:38,720 --> 00:04:40,250
simple to set up a list of you.

71
00:04:40,460 --> 00:04:41,990
You just connect the model and boom.

72
00:04:41,990 --> 00:04:47,690
By default, it's going to do a query request for all the objects inside that model and then just send

73
00:04:47,690 --> 00:04:48,230
them back.

74
00:04:48,500 --> 00:04:55,310
And what it does is it sends them back as this object underscore list now object underscore list.

75
00:04:55,730 --> 00:05:01,340
This can be a little cryptic if maybe you have a really confusing set of views.

76
00:05:01,350 --> 00:05:05,660
Not really confusing, but let's say you have a lot of use and you just have a front end developer who's

77
00:05:05,660 --> 00:05:08,030
just looking at this and they start scratching their head.

78
00:05:08,030 --> 00:05:09,440
What is object list?

79
00:05:09,830 --> 00:05:13,580
What you could do is actually change this variable name.

80
00:05:14,120 --> 00:05:19,700
The way you do that is you come back to views and then here on their teacher list view what you can

81
00:05:19,700 --> 00:05:22,280
say is context.

82
00:05:23,420 --> 00:05:25,570
Object name.

83
00:05:26,270 --> 00:05:32,120
And then you pass in a string of what you actually want to have as the object name, so then you could

84
00:05:32,120 --> 00:05:40,910
say something like teacher list and then instead of the object list, you could call this teacher list.

85
00:05:41,750 --> 00:05:43,130
So let's confirm that worked.

86
00:05:43,580 --> 00:05:48,530
You should be able to just come back here and refresh this and you see, have the teachers working again.

87
00:05:48,860 --> 00:05:52,040
And if you ever want, you can come back here, create a new teacher.

88
00:05:52,340 --> 00:05:54,230
Let's call this third teacher.

89
00:05:55,250 --> 00:05:57,800
Teaches math, submit.

90
00:05:58,430 --> 00:06:02,510
Come back to the classroom page, and we may want to put a link to the home page, and we'll be doing

91
00:06:02,510 --> 00:06:05,600
that a lot and then we can go to last page here.

92
00:06:05,660 --> 00:06:11,660
Jose Arrowsmith, third teacher So it looks like that's all working again that allows you to edit what

93
00:06:11,660 --> 00:06:13,250
this particular variable name is.

94
00:06:13,370 --> 00:06:18,290
I would recommend that you typically overwrite that object list can be a little confusing, especially

95
00:06:18,290 --> 00:06:21,050
as you get more and more views and more and more HTML files.

96
00:06:21,050 --> 00:06:24,920
So it's much easier to understand what teacher list is than what object list is.

97
00:06:25,280 --> 00:06:31,940
OK, so the other thing on a point out is you actually have a lot of flexibility over what actual query

98
00:06:31,970 --> 00:06:35,450
is created to grab all the instances in this model.

99
00:06:36,200 --> 00:06:37,190
So what do I mean by that?

100
00:06:37,370 --> 00:06:45,560
Well, what you could do is call query set, and let's make sure we spell that right.

101
00:06:45,680 --> 00:06:48,170
Query set is equal to.

102
00:06:48,560 --> 00:06:53,090
And here you can override the default query.

103
00:06:53,570 --> 00:06:56,350
By default, your query looks something like this.

104
00:06:56,360 --> 00:06:59,060
It's just saying teacher objects.

105
00:06:59,480 --> 00:07:03,350
All this is essentially the default query set that a list view does.

106
00:07:03,680 --> 00:07:08,540
But perhaps you may want to actually sort or order the teachers.

107
00:07:08,870 --> 00:07:10,430
So it's really up to you.

108
00:07:10,460 --> 00:07:13,730
What other factors you may want inside this query set?

109
00:07:14,090 --> 00:07:17,210
In fact, later on then you can have multiple lists for different factors.

110
00:07:17,510 --> 00:07:23,090
But what you do is you override the actual query set that ends up being sent back as this context object.

111
00:07:23,450 --> 00:07:29,810
So, for example, I can say teacher the objects and then say something like, well, actually order

112
00:07:29,810 --> 00:07:32,900
by an order by, let's say, their first name.

113
00:07:34,130 --> 00:07:39,650
And this is where you can use all the skills you learned back in the model section on things about querying

114
00:07:39,650 --> 00:07:45,200
or filtering, etc. You can use that filter that order by whatever you want really to override this

115
00:07:45,200 --> 00:07:46,450
query set that way.

116
00:07:46,460 --> 00:07:50,600
Maybe you don't want to actually list every single teacher, but only maybe certain teachers or something

117
00:07:50,600 --> 00:07:51,050
like that.

118
00:07:51,350 --> 00:07:55,640
So you just override the query set attribute, go ahead and save that change.

119
00:07:55,820 --> 00:07:59,840
And then I'm going to come back here to list the teachers and to refresh this, and you can see now

120
00:07:59,840 --> 00:08:00,830
it's in alphabetical order.

121
00:08:00,860 --> 00:08:02,180
A J.

122
00:08:02,540 --> 00:08:03,200
And T..

123
00:08:04,070 --> 00:08:04,340
OK.

124
00:08:04,850 --> 00:08:09,410
So those are the three main attributes that you should know about for a list view.

125
00:08:09,470 --> 00:08:13,760
Again, the one that's required is to connect it to a model, and we saw technically a one liner.

126
00:08:13,760 --> 00:08:14,660
That's all you need to do.

127
00:08:14,990 --> 00:08:20,560
But then if you want to edit the context, object name that's passed back to the model underscore list

128
00:08:20,570 --> 00:08:21,150
h html.

129
00:08:21,170 --> 00:08:25,910
You can overwrite that this attribute, and you can even overwrite the actual query set on what does

130
00:08:25,910 --> 00:08:28,160
actually mean by a list of everything.

131
00:08:28,610 --> 00:08:29,000
OK.

132
00:08:29,450 --> 00:08:29,990
Pretty cool.

133
00:08:30,410 --> 00:08:35,419
Now, so far we have the ability to create a new teacher and list all the teachers.

134
00:08:35,720 --> 00:08:40,130
But imagine I wanted to be able to get the specific details of a teacher.

135
00:08:40,490 --> 00:08:41,120
Notice here.

136
00:08:41,120 --> 00:08:46,760
I don't actually see what each teacher ends up teaching as a subject, so maybe I want to actually get

137
00:08:46,760 --> 00:08:48,860
what's known as a detail view.

138
00:08:49,220 --> 00:08:50,840
We'll learn about that in the next lecture.

