1
00:00:05,300 --> 00:00:09,650
Welcome back, everyone, to this lecture on the creative class based view.

2
00:00:10,840 --> 00:00:15,130
Now, before we continue, I do want to point out that the next few lectures are going to highlight

3
00:00:15,160 --> 00:00:20,260
what I think is one of the best features of class based views, and those are what I like to call the

4
00:00:20,260 --> 00:00:22,360
model based, class based views.

5
00:00:22,750 --> 00:00:25,630
These are the ones that directly connect with their model.

6
00:00:26,050 --> 00:00:30,520
So there's a couple of operations that are super common when you build out a model on the back end,

7
00:00:30,820 --> 00:00:35,410
things like creating a new instance in that model, maybe a detailed view for a particular instance

8
00:00:35,410 --> 00:00:40,810
of that model being able to update, in instance, delete an instance or lists a bunch of instances

9
00:00:40,810 --> 00:00:44,380
or all the instances from any particular model in your database.

10
00:00:45,760 --> 00:00:51,730
What Django provides for us are class based views that automatically create the appropriate views,

11
00:00:51,730 --> 00:00:57,550
forms and context objects for predefined template names by simply being connected to a model.

12
00:00:57,880 --> 00:01:03,100
And these classes only require a few attributes and then they automatically do the work for you.

13
00:01:03,370 --> 00:01:07,480
It's pretty amazing how much time these set of classes can actually save you.

14
00:01:08,880 --> 00:01:14,160
As a quick, important note, because the classes are designed to be simple, that is simple to use.

15
00:01:14,430 --> 00:01:18,240
These views require a template name to follow a very specific pattern.

16
00:01:18,570 --> 00:01:24,000
For example, the if you were about to discover is the create view, and that actually requires you

17
00:01:24,030 --> 00:01:29,490
to create a template name in the specific pattern of model underscore form that each HTML.

18
00:01:29,820 --> 00:01:35,280
So for example, if we created a teacher model, then our template name has to be teacher underscore

19
00:01:35,280 --> 00:01:36,780
form that each HTML.

20
00:01:37,440 --> 00:01:41,640
I just want to point this out because this factor is often confusing for students because it seems like

21
00:01:41,640 --> 00:01:45,120
Django just magically knew a template HTML file name existed.

22
00:01:45,330 --> 00:01:48,990
But really, what it's doing is just looking for a specific naming convention pattern.

23
00:01:49,990 --> 00:01:50,350
OK.

24
00:01:50,710 --> 00:01:55,720
Let's begin our journey of model based lives by starting with Create View, which automatically creates

25
00:01:55,720 --> 00:01:59,830
a view to create a new instance of a model class by the client on the user side.

26
00:02:00,370 --> 00:02:06,580
OK, so here I am inside a visual studio code as a quick note, recall that previously we created a

27
00:02:06,580 --> 00:02:13,300
contact form view and we saw that I could easily connect a form and then actually have a class based

28
00:02:13,300 --> 00:02:14,020
view for that.

29
00:02:14,470 --> 00:02:18,970
We've also known that there's technically something that exists called a model form.

30
00:02:19,330 --> 00:02:24,100
So you may have been thinking, Oh, this is super convenient, I'll simply create a model form and

31
00:02:24,100 --> 00:02:26,590
then connect that with a class based view.

32
00:02:27,040 --> 00:02:32,110
Now, obviously, that is apparent enough that jingo figured it would do that work for you.

33
00:02:32,440 --> 00:02:37,480
So in the same way, you can use a model form to quickly connect a form to model and then technically

34
00:02:37,480 --> 00:02:39,880
use a form view to connect to that model form.

35
00:02:40,120 --> 00:02:44,620
You could just skip all the steps and directly connect a class based view to a model.

36
00:02:44,980 --> 00:02:47,530
The first one we're going to discover is the create view.

37
00:02:48,010 --> 00:02:54,370
Go ahead and import create view from the jingo that views generic.

38
00:02:54,820 --> 00:02:57,460
And then I'll show you right now how to set up a create view.

39
00:02:58,030 --> 00:03:02,380
So underneath, thank you view, I'll create a new class.

40
00:03:04,690 --> 00:03:08,500
And this is going to be called teacher create view.

41
00:03:08,980 --> 00:03:12,820
Technically speaking, you can call these classes whatever you want, but I would highly recommend you

42
00:03:12,820 --> 00:03:18,550
follow the convention of the name of your model and then the name of the template view or whatever your

43
00:03:18,550 --> 00:03:23,620
view happens to be, like create, view, form, view, etc. So we're right now discovering the model

44
00:03:23,620 --> 00:03:24,760
based, class based view.

45
00:03:24,790 --> 00:03:27,280
So here we have the model name and then create view.

46
00:03:27,880 --> 00:03:30,430
I'm going to inherit, create, view.

47
00:03:31,600 --> 00:03:35,140
And then there's just a few attributes that we need to connect it to, and he first need to tell it.

48
00:03:35,320 --> 00:03:37,840
Well, what model are you actually going to connect to?

49
00:03:38,260 --> 00:03:40,810
And I'm going to connect it to the teacher model.

50
00:03:41,200 --> 00:03:45,620
Now, no, I have an imported teacher model yet, so I need to do that at the top of my script here.

51
00:03:46,180 --> 00:03:47,440
So I'm going to say from.

52
00:03:48,810 --> 00:03:57,210
Classroom thought models in poor, the teacher model that we created earlier, so recall that we've

53
00:03:57,210 --> 00:04:03,000
already created the teacher model inside models that Pi first name, last name, the subject they teach

54
00:04:03,000 --> 00:04:05,070
and there's a string representation of that model.

55
00:04:05,910 --> 00:04:09,270
OK, so coming back to our views.

56
00:04:10,900 --> 00:04:11,350
They were go.

57
00:04:12,430 --> 00:04:18,220
Coming back to our views, step one, connect it to the model, step two for create view is to figure

58
00:04:18,220 --> 00:04:23,380
out what field do you actually want to display in your create view, and this looks really similar to

59
00:04:23,380 --> 00:04:24,070
model forms.

60
00:04:24,400 --> 00:04:26,150
So it's essentially kind of the same attribute.

61
00:04:26,500 --> 00:04:28,660
If you want, you could just pass on a list here.

62
00:04:28,670 --> 00:04:31,930
That's something like, OK, first name field.

63
00:04:32,230 --> 00:04:34,570
Also do the last name field, et cetera.

64
00:04:34,900 --> 00:04:40,660
Maybe there's some fields you don't want the user to have access to, but typically you'll often just

65
00:04:40,660 --> 00:04:43,660
say, I want all the fields to show up in my create view.

66
00:04:43,960 --> 00:04:49,000
So you just do what we did in the model form, which was underscore, underscore, all underscore,

67
00:04:49,000 --> 00:04:49,540
underscore.

68
00:04:49,930 --> 00:04:55,120
OK, so you connected it to the model, you connected it and said, I want all the fields to show up

69
00:04:55,120 --> 00:04:57,340
in this template when they're actually creating that form.

70
00:04:57,730 --> 00:05:01,180
And then finally, remember, this is technically a creation view.

71
00:05:01,210 --> 00:05:05,590
So this is going to be a form that shows up in order to create a new instance of a teacher.

72
00:05:05,950 --> 00:05:10,360
Now, obviously, when you hit submit on that form, we should probably take you to a success URL.

73
00:05:11,140 --> 00:05:14,950
So that's the other attribute we need to define success URL.

74
00:05:15,340 --> 00:05:17,680
And let's just take them to the thank you page again.

75
00:05:17,890 --> 00:05:22,120
We already know how to do that based off our example here, which was reverse lazy classroom.

76
00:05:22,120 --> 00:05:22,480
Thank you.

77
00:05:22,900 --> 00:05:25,570
So it's simply copy that and paste that in.

78
00:05:26,110 --> 00:05:30,850
And that's pretty much all we really have to do to get great view working again.

79
00:05:31,090 --> 00:05:35,770
You simply connect it to a model, tell it what fields you want to show up and the template when it

80
00:05:35,770 --> 00:05:41,980
passes back that creation form and then the success URL, which in this case is just reverse lazy and

81
00:05:41,980 --> 00:05:44,320
then the URL you want to point after they hit submit.

82
00:05:45,070 --> 00:05:50,380
Now again, a question students often ask is how does it actually know what template to connect to notice?

83
00:05:50,500 --> 00:05:52,000
I didn't actually say template name.

84
00:05:52,420 --> 00:05:54,400
Well, create view by default.

85
00:05:54,610 --> 00:06:02,410
Once you actually tell it the model the template name is going to look for is the model underscore form

86
00:06:02,410 --> 00:06:06,640
that each HTML file inside the same application that the views is in.

87
00:06:07,000 --> 00:06:13,240
So what we need to do is create that particular template and the model is going to be in all lowercase.

88
00:06:13,240 --> 00:06:14,650
So it's going to be lowercase T here.

89
00:06:16,210 --> 00:06:22,600
And so we'll come back to classroom, create a new file and call it teacher underscore.

90
00:06:23,110 --> 00:06:30,730
Form the HTML and now Django knows I'm going to look up for model underscore form and find the connection

91
00:06:30,730 --> 00:06:31,000
here.

92
00:06:31,480 --> 00:06:34,350
And then it's kind of up to you where you want to put for a header here.

93
00:06:34,360 --> 00:06:37,120
I'll just say teacher form.

94
00:06:37,480 --> 00:06:40,780
I'm actually not going to specify create form because you're going to see other views are actually going

95
00:06:40,780 --> 00:06:42,440
to share this same template.

96
00:06:42,700 --> 00:06:48,310
Because imagine if you wanted to update a teacher, it would actually be useful to just use that same

97
00:06:48,310 --> 00:06:48,640
form.

98
00:06:49,300 --> 00:06:51,880
And then we use a form past that in.

99
00:06:52,420 --> 00:06:55,750
I'm going to say the method is equal to post.

100
00:06:57,100 --> 00:06:59,260
And then we pass the necessary token.

101
00:07:00,540 --> 00:07:03,180
And then pass in form that as.

102
00:07:04,690 --> 00:07:07,510
And then we simply need our input button.

103
00:07:07,600 --> 00:07:10,060
So we'll say input submit.

104
00:07:10,660 --> 00:07:12,440
I'll put the value of the text as submit.

105
00:07:13,030 --> 00:07:13,870
And there you go.

106
00:07:13,880 --> 00:07:14,950
That's basically all you need.

107
00:07:15,660 --> 00:07:21,330
Automatically, what's going to happen is teacher create view looks up this particular template.

108
00:07:21,340 --> 00:07:25,670
So in that case, teacher underscore form and it figures, OK, these are the fields you want.

109
00:07:25,690 --> 00:07:30,250
I'll pretty much auto create a model form for you, and then we'll set that up.

110
00:07:30,700 --> 00:07:36,670
And what's really cool about this is the second you hit the submit button, it's pretty much automatically

111
00:07:36,700 --> 00:07:38,650
going to hit save.

112
00:07:38,650 --> 00:07:42,400
After all, the fields are validated, so it's doing a lot of work for you.

113
00:07:43,150 --> 00:07:49,660
But the caveat with that work is this view is really only for one thing, it's creating a new instance

114
00:07:49,660 --> 00:07:50,710
in that model.

115
00:07:51,040 --> 00:07:52,330
Let's explore how this works.

116
00:07:52,840 --> 00:07:55,620
So I'm going to come back to my home page.

117
00:07:56,140 --> 00:07:57,490
So we're going to come back here.

118
00:07:57,730 --> 00:07:59,530
And in fact, let's link it to our home page.

119
00:07:59,530 --> 00:08:02,980
So I'm going to come back to home here, and let's just copy and paste this.

120
00:08:03,670 --> 00:08:09,430
You'll see we're going to be like gradually expanding this and let's say classroom contact and actually

121
00:08:09,760 --> 00:08:12,790
connect to this create view to a URL, yet only to do that as well.

122
00:08:13,210 --> 00:08:18,520
So we'll actually say contact and this will be called creates, let's say, create teacher.

123
00:08:19,480 --> 00:08:21,550
And this will be Create Teacher page.

124
00:08:23,000 --> 00:08:27,350
So create teacher and just to specify creates new teacher.

125
00:08:28,070 --> 00:08:34,789
OK, so then what I'm going to do is come back to you URLs and imports that teacher create view.

126
00:08:36,250 --> 00:08:38,950
And let's set it up as a new view here, so we'll say Path.

127
00:08:39,789 --> 00:08:42,549
I'm going to call this creates teacher.

128
00:08:44,039 --> 00:08:46,830
I call my teacher create view as view.

129
00:08:47,550 --> 00:08:51,840
And let's give it the name that we said was create underscore teacher.

130
00:08:53,060 --> 00:08:59,530
OK, so now I have this class based view, which is a create view, I connected it to a model.

131
00:08:59,540 --> 00:09:04,670
I told it what fields I wanted and I told it where to go after I successfully submitted a new teacher.

132
00:09:04,940 --> 00:09:10,700
And what that's going to do for me is all the heavy lifting of inserting this new teacher into the database.

133
00:09:10,700 --> 00:09:16,130
And really, keep in mind, it's just doing it from three attributes here, which is really incredible

134
00:09:16,130 --> 00:09:17,810
at how much time is going to save you later on.

135
00:09:18,650 --> 00:09:21,260
So I'm going to come back to my home page here.

136
00:09:21,770 --> 00:09:22,760
Let me refresh.

137
00:09:23,030 --> 00:09:24,440
Let's make sure we get a typo here.

138
00:09:24,710 --> 00:09:26,240
Create new teacher page link.

139
00:09:26,660 --> 00:09:27,470
I click on that.

140
00:09:27,710 --> 00:09:31,790
That takes it to the URL classroom forward slash create underscore teacher.

141
00:09:32,180 --> 00:09:33,500
And then let's create a new teacher.

142
00:09:34,010 --> 00:09:38,450
So I will create a new teacher here, and let's actually make the subject go.

143
00:09:38,810 --> 00:09:39,140
OK.

144
00:09:39,620 --> 00:09:43,450
So what's really interesting is you have now this form it's automatically created for you.

145
00:09:43,460 --> 00:09:46,110
Remember Insight Teacher underscore form.

146
00:09:46,130 --> 00:09:48,530
I'm basically just passing in form and simple view.

147
00:09:48,830 --> 00:09:52,610
Obviously, you could style this however you want it, but that's all you had to do at the minimum.

148
00:09:53,150 --> 00:09:56,390
And then when you hit submit, it takes you to the thank you page.

149
00:09:57,110 --> 00:10:03,980
Now what I could do is actually confirm that that teacher has been created in order to confirm that

150
00:10:03,980 --> 00:10:05,090
the teacher has been created.

151
00:10:05,480 --> 00:10:10,700
I will need to either create a super user and then go to the admin to view that it's been created.

152
00:10:11,000 --> 00:10:14,840
Or I'll need to create another view that ends up listing all my teachers.

153
00:10:15,260 --> 00:10:20,240
So what you can do if you want, if you kind of don't believe that it worked is right now just create

154
00:10:20,240 --> 00:10:21,650
a super user in the admin.

155
00:10:22,070 --> 00:10:27,590
But what I'm going to do is in the very next lecture, I'm going to show you the next model based view,

156
00:10:27,620 --> 00:10:33,320
which is a list view in order to list all the entries for a particular model.

157
00:10:33,740 --> 00:10:35,210
So hold off on this right now.

158
00:10:35,220 --> 00:10:39,620
Just take it on my word that it actually worked, but I'll prove it to you in the very next lecture

159
00:10:39,620 --> 00:10:40,340
through a list of you.

160
00:10:40,670 --> 00:10:41,180
I'll see you there.

