1
00:00:05,310 --> 00:00:10,560
Welcome back, everyone, in this lecture, we're going to explore the elite view, which allows us

2
00:00:10,560 --> 00:00:12,270
to delete an instance from a model.

3
00:00:13,020 --> 00:00:13,290
All right.

4
00:00:13,320 --> 00:00:19,290
Here I am inside our views that PI file in order to create a delete a view, we need to import that.

5
00:00:19,590 --> 00:00:24,600
So we'll say again from jangled that views generic import deletes view.

6
00:00:25,200 --> 00:00:27,440
This is a kind of a simple view.

7
00:00:27,450 --> 00:00:32,640
Basically, you just pointed to a primary key and then it has a one button form that says, Are you

8
00:00:32,640 --> 00:00:35,430
sure you want to delete this particular instance of the model?

9
00:00:35,910 --> 00:00:37,170
So let's set it up.

10
00:00:37,890 --> 00:00:42,570
We're going to create a class here, and I'm going to call it my teacher.

11
00:00:43,780 --> 00:00:49,510
Delete a view, and then we inherits from the view, we connect it to the model.

12
00:00:50,640 --> 00:00:57,540
And then keep in mind, the elite view is technically a form, it sends back a form that just has a

13
00:00:57,540 --> 00:01:05,160
single confirmed delete button because keep in mind if you're already on the deletes view, you're not

14
00:01:05,160 --> 00:01:10,740
looking to actually read all the fields of information, like on a detailed view or change any particular

15
00:01:10,740 --> 00:01:11,190
aspect.

16
00:01:11,220 --> 00:01:14,610
You basically just have a button, says, Hey, you're looking at this particular teacher, are you

17
00:01:14,610 --> 00:01:15,570
sure you want to delete them?

18
00:01:15,990 --> 00:01:21,690
So we're just going to say form go to confirm delete button, and we just simply connect it to a teacher.

19
00:01:21,700 --> 00:01:25,140
But because it is a form, we also need a success URL.

20
00:01:25,170 --> 00:01:30,300
I'm going to copy the same one we used for up the view because after deleting the teacher, we'll want

21
00:01:30,300 --> 00:01:33,000
to confirm that by going to the list of all the teachers.

22
00:01:33,390 --> 00:01:35,640
So we'll go ahead and save that change there.

23
00:01:36,480 --> 00:01:36,810
OK.

24
00:01:37,140 --> 00:01:38,730
So we have our daily view working.

25
00:01:38,730 --> 00:01:40,530
Let's connect it to our URLs.

26
00:01:41,040 --> 00:01:42,390
Will come to URLs.

27
00:01:42,420 --> 00:01:48,870
Remember to import teacher, delete, view and then create a new path here.

28
00:01:50,120 --> 00:01:51,990
And it's kind of up to you what you want to call this path.

29
00:01:52,010 --> 00:01:53,900
I'll call it deletes, underscore teacher.

30
00:01:54,320 --> 00:01:56,630
But remember, we don't want to just delete any old teacher.

31
00:01:56,630 --> 00:01:58,400
We have to delete a specific teacher.

32
00:01:58,400 --> 00:02:00,740
So we say integer primary key.

33
00:02:00,740 --> 00:02:03,110
Just read that for update and detail.

34
00:02:03,800 --> 00:02:08,630
And then we pass in teacher delete to view as a view.

35
00:02:09,080 --> 00:02:10,479
And if you want, you can give it a name.

36
00:02:10,490 --> 00:02:11,510
We don't use a name here.

37
00:02:12,080 --> 00:02:16,850
OK, so we have our delete teacher and then we need to set up our actual template.

38
00:02:17,360 --> 00:02:23,340
So the way this works is this once something you will kind of need to remember is a delete view.

39
00:02:23,510 --> 00:02:30,860
The default template name that it looks for is going to be your model underscore, confirm, underscore,

40
00:02:31,100 --> 00:02:32,510
delete the HTML.

41
00:02:32,990 --> 00:02:40,940
So in our case, that's going to be creating a new file with the name teacher underscore, confirm,

42
00:02:40,940 --> 00:02:43,310
underscore, delete the HTML.

43
00:02:43,760 --> 00:02:45,920
So that's what it automatically looks for.

44
00:02:46,310 --> 00:02:53,150
And in this case, you basically just have to create a form and inside that form, it's going to be

45
00:02:53,150 --> 00:02:54,080
a post method.

46
00:02:54,320 --> 00:02:57,260
So we'll say method is equal to post.

47
00:02:59,070 --> 00:03:02,280
And you need to pass in the seats are a token for security.

48
00:03:03,350 --> 00:03:09,440
And then you have a submit input button, and here maybe you want the value instead of being submit

49
00:03:09,440 --> 00:03:13,340
the value of the actual text inside the button to be confirm.

50
00:03:14,580 --> 00:03:21,120
Like delete or something like that, and you should probably have some information here of saying something

51
00:03:21,120 --> 00:03:27,030
like, are you sure you want to delete this teacher?

52
00:03:27,600 --> 00:03:29,980
And what's also nice about the delete view?

53
00:03:30,030 --> 00:03:36,350
It does send back an object of the teacher so you can actually grab information from it.

54
00:03:36,360 --> 00:03:41,610
So if you want it to, you could just to inform the person viewing this page of the teacher.

55
00:03:42,000 --> 00:03:42,330
OK.

56
00:03:42,660 --> 00:03:47,100
So that's why it's kind of similar to a detailed view and the fact that it is sending back this teacher.

57
00:03:47,310 --> 00:03:53,130
Except it also sends back the idea of this form that can then have this confirmation of the leak notice,

58
00:03:53,130 --> 00:03:55,490
how it doesn't actually send any labels or inputs.

59
00:03:55,500 --> 00:03:58,050
It's just a form that says, Hey, why don't you submit this?

60
00:03:58,290 --> 00:04:02,520
I'm going to go and grab this teacher in the database and then remove them through a delete call.

61
00:04:02,970 --> 00:04:05,640
So this is teacher underscore, confirm, delete.

62
00:04:06,840 --> 00:04:11,880
And that is how the elite view works, you connect it to a model and then take it to a success URL.

63
00:04:12,310 --> 00:04:17,440
And remember inside this URLs, I need to say delete feature with that particular integer call.

64
00:04:17,459 --> 00:04:18,690
That is the primary key.

65
00:04:19,170 --> 00:04:22,650
Then the question arises is where do we actually want to link to the teacher?

66
00:04:23,070 --> 00:04:26,760
Well, it probably makes sense to link it in the same way we linked to Update Teacher.

67
00:04:27,240 --> 00:04:34,800
So I'm going to go back to my teacher list view, and in the same way, I had this unordered list to

68
00:04:34,800 --> 00:04:36,720
update the information for the teacher.

69
00:04:37,230 --> 00:04:41,090
Let's create another nested unordered list here.

70
00:04:42,050 --> 00:04:45,950
So we'll go ahead and essentially copy this.

71
00:04:47,940 --> 00:04:49,500
And pasted again.

72
00:04:52,230 --> 00:04:53,790
So we'll put it in here.

73
00:04:54,210 --> 00:04:58,890
And in fact, this actually doesn't really need to be nested again or it's going to create another list

74
00:04:58,890 --> 00:05:01,590
item inside that same unordered list.

75
00:05:02,130 --> 00:05:02,610
There we go.

76
00:05:02,640 --> 00:05:07,290
So we'll have the teacher and then nested inside will be, Hey, do you want up the information or do

77
00:05:07,290 --> 00:05:08,040
you want to?

78
00:05:09,020 --> 00:05:11,360
Delete that teacher, so delete.

79
00:05:14,740 --> 00:05:16,000
And then save that.

80
00:05:16,210 --> 00:05:16,540
OK.

81
00:05:16,930 --> 00:05:23,080
So what this is saying is go ahead and go to delete teacher URL for that particular teacher I.D., which

82
00:05:23,080 --> 00:05:26,490
means it would take you to the late teacher URL for that particular I.D..

83
00:05:26,740 --> 00:05:32,590
Which brings back a teacher delete view, which we know just connects to the teacher model and calls

84
00:05:32,860 --> 00:05:33,640
model confirmed.

85
00:05:33,640 --> 00:05:37,720
Delete that email, which we have here, that basically says, Hey, are you sure you want to delete

86
00:05:37,720 --> 00:05:38,320
this teacher?

87
00:05:38,770 --> 00:05:40,160
Here's the teacher going about to delete.

88
00:05:40,230 --> 00:05:40,930
Here's the form.

89
00:05:41,140 --> 00:05:41,830
Click Submit.

90
00:05:42,010 --> 00:05:47,090
And I'll go ahead and execute the deletion of that particular instance of the model.

91
00:05:47,110 --> 00:05:48,060
So let's check this all out.

92
00:05:48,070 --> 00:05:49,150
Make sure we didn't commit a typo.

93
00:05:49,510 --> 00:05:50,560
Then it refresh this.

94
00:05:50,920 --> 00:05:56,470
I'll go to this teacher page link and here it, says Adam Smith up the information for Adam Audley Adam.

95
00:05:56,740 --> 00:06:01,240
Obviously, we want to use some sort of more like secure coloring or really, it depends on your website

96
00:06:01,270 --> 00:06:03,550
of how you actually want to connect the deletion.

97
00:06:03,550 --> 00:06:05,290
But let's go to the third teacher here.

98
00:06:05,590 --> 00:06:10,090
I'll click Delete Third and it says, Hey, are you sure you want to delete this teacher?

99
00:06:10,600 --> 00:06:15,580
Third Teacher teaches math and I'm going to confirm delete and you notice it no longer shows up there.

100
00:06:15,610 --> 00:06:17,140
Let's go ahead and delete Adam Smith.

101
00:06:18,200 --> 00:06:19,400
That says, hey, are you sure you want to?

102
00:06:19,610 --> 00:06:24,020
The teacher, Adam Smith, teaches math and again, it makes sense that the form is just a single button

103
00:06:24,020 --> 00:06:26,690
form because what else information do you need to provide?

104
00:06:26,870 --> 00:06:30,470
It already knows the primary key automatically through the use of the class based view.

105
00:06:30,920 --> 00:06:32,510
So you click the lead and then boom.

106
00:06:33,080 --> 00:06:36,830
OK, so that's really it for our class based views.

107
00:06:37,130 --> 00:06:38,780
A very, very powerful tool.

108
00:06:39,140 --> 00:06:45,620
Again, you have the ability if we just kind of go back here through class based views, two in two

109
00:06:45,620 --> 00:06:49,670
lines connect to a template awesome through essentially three lines.

110
00:06:49,670 --> 00:06:53,180
Connect to a creation view where the creation is just a form.

111
00:06:53,570 --> 00:06:58,730
Then you can also again through three lines of attributes, just list all the instances and you have

112
00:06:58,730 --> 00:07:01,970
the flexibility here for the query set of how you want the list them.

113
00:07:02,390 --> 00:07:07,730
You have the ability to show a specific detail to update a specific instance and then delete a specific

114
00:07:07,730 --> 00:07:08,210
instance.

115
00:07:08,480 --> 00:07:14,810
And those are the key model based, class based views that are going to make your development life on

116
00:07:14,810 --> 00:07:18,290
The View's side of thing a lot more smoother.

117
00:07:18,290 --> 00:07:18,860
Comfortable.

118
00:07:19,280 --> 00:07:24,410
So we have the template, form, view, create, view, list, view, detailed view, update, view

119
00:07:24,410 --> 00:07:25,100
and The View.

120
00:07:25,370 --> 00:07:28,580
I would highly recommend that you use these as often as possible.

121
00:07:28,970 --> 00:07:36,380
The last caveat being is not every single view that you could possibly think of is ideally a class based

122
00:07:36,380 --> 00:07:36,680
view.

123
00:07:36,710 --> 00:07:41,300
There may be times where it just makes a lot more sense to write the custom function and take care of

124
00:07:41,300 --> 00:07:41,840
it that way.

125
00:07:42,110 --> 00:07:47,840
So this is also laid out in the Django documentation that there is not some magical class based view

126
00:07:47,840 --> 00:07:49,880
that exists for your specific situation.

127
00:07:50,180 --> 00:07:55,010
If it comes down to it, you can always go back to the function based view and add in a lot more logic.

128
00:07:55,280 --> 00:08:00,680
Remember, with the simplicity and ease of use of the class based views comes a lot more constraints

129
00:08:00,680 --> 00:08:01,340
and caveats.

130
00:08:01,610 --> 00:08:05,360
And if you ever find yourself outside of those constraints, you can always go back to the function

131
00:08:05,360 --> 00:08:05,570
based.

132
00:08:05,630 --> 00:08:07,430
You hope that was useful for you.

133
00:08:07,520 --> 00:08:08,480
I'll see you at the next lecture.

