1
00:00:05,330 --> 00:00:10,580
Welcome back, everyone, to this lecture, which is going to discuss the update view, which allows

2
00:00:10,580 --> 00:00:17,480
us to update an already existing entry inside our models, similar to create view, except for something

3
00:00:17,480 --> 00:00:18,470
that already exists.

4
00:00:18,830 --> 00:00:19,520
Let's get started.

5
00:00:20,240 --> 00:00:20,510
All right.

6
00:00:20,540 --> 00:00:23,480
Here I am back inside views about pie.

7
00:00:23,900 --> 00:00:30,710
One thing to note when we talk about update view is it's kind of a mix between a detailed view and a

8
00:00:30,710 --> 00:00:31,610
create view.

9
00:00:32,090 --> 00:00:36,590
It's kind of like a create view because you will be filling out a form to up the information.

10
00:00:36,890 --> 00:00:41,420
But it's also kind of like a detail view because when you're talking about updating, you're talking

11
00:00:41,420 --> 00:00:44,570
about a specific entry with a unique primary key.

12
00:00:44,990 --> 00:00:51,110
So you can think of an update view as a create view that already pre filled out the form based off a

13
00:00:51,110 --> 00:00:51,800
detailed view.

14
00:00:52,130 --> 00:00:53,030
Let's see how it works.

15
00:00:53,570 --> 00:01:00,710
I'm going to import update view from Generic, and then let's create our new update view.

16
00:01:01,370 --> 00:01:04,849
So we'll say the following class.

17
00:01:06,550 --> 00:01:17,650
Teacher up, the view, we inherit the up the view and then we say model is equal to teacher, so we

18
00:01:17,650 --> 00:01:23,290
connect it to a model and then technically there may only be a few fields you want to update.

19
00:01:23,800 --> 00:01:31,930
However, you should note the following is that Teacher Update View is actually going to share the model

20
00:01:31,960 --> 00:01:39,700
form HTML template that create View also uses, and it looks a lot like create view as far as attributes

21
00:01:39,700 --> 00:01:40,300
are concerned.

22
00:01:40,960 --> 00:01:43,820
Now, let's take a moment and think, does this actually make sense?

23
00:01:43,840 --> 00:01:48,250
Should it make sense that the creation view uses the same form as the update view?

24
00:01:48,520 --> 00:01:53,800
In my opinion, it's yes, that does make sense, because if you're creating a new instance of a teacher,

25
00:01:54,100 --> 00:01:58,000
you should have all those fields available for you when you want to update a teacher.

26
00:01:58,480 --> 00:02:01,720
Now, maybe you want to limit the fields upon updating.

27
00:02:02,020 --> 00:02:03,310
You can technically do that.

28
00:02:03,340 --> 00:02:08,259
You could say fields and your update view is equal to and then pass a list.

29
00:02:08,680 --> 00:02:13,960
For example, maybe you're not going to, for whatever reason, allow people to update their first name

30
00:02:14,410 --> 00:02:15,610
or the subject they teach.

31
00:02:15,610 --> 00:02:20,560
But maybe you want to be able to update last names in case somebody gets married and changes their last

32
00:02:20,560 --> 00:02:20,830
name.

33
00:02:21,220 --> 00:02:26,980
What you could do is have an update view that only shows the last name on the form, and you could just

34
00:02:26,980 --> 00:02:31,390
pass on a list of the fields and the names you want to update.

35
00:02:31,570 --> 00:02:33,190
And remember these names here.

36
00:02:33,460 --> 00:02:36,670
Those are just from the model, so I connected it to Model Teacher.

37
00:02:37,090 --> 00:02:42,790
I go to models and I see, OK, I could update their first name, last name, subject, or if you just

38
00:02:42,790 --> 00:02:48,010
want to be able to update all of them, you would come back to views and just like we did for create

39
00:02:48,010 --> 00:02:48,340
view.

40
00:02:48,850 --> 00:02:55,390
You would just as a string pass, an underscore underscore all that says allow all the fields to show

41
00:02:55,390 --> 00:02:59,650
up in the form that's automatically filled out and created for update view.

42
00:03:00,130 --> 00:03:06,340
And then lastly, similar to our creation view, remember, upon clicking submit for the update, we

43
00:03:06,340 --> 00:03:10,390
have to take them to a particular page so we could use the same idea of success.

44
00:03:10,390 --> 00:03:13,390
URL reverse lazy to do that here.

45
00:03:13,390 --> 00:03:14,260
An update view.

46
00:03:15,330 --> 00:03:21,270
OK, so we say success early, except maybe we take them to the list teachervue, because that would

47
00:03:21,270 --> 00:03:23,560
make more sense to actually view the changes.

48
00:03:24,000 --> 00:03:29,700
So this way we'll update you does is it's actually going to open up the.

49
00:03:30,810 --> 00:03:36,750
Teacher form and then display the form that you sent back, obviously, as I mentioned, you technically

50
00:03:36,750 --> 00:03:42,300
have flexibility over what fields show up an update versus what fields show up and create, and in certain

51
00:03:42,300 --> 00:03:43,890
situations, that actually makes a lot of sense.

52
00:03:44,160 --> 00:03:48,750
Do you have limitations on what can be updated versus limitations on what can be created?

53
00:03:49,110 --> 00:03:54,330
Maybe there are some sort of date time stamp for creation that you don't want someone to be able to

54
00:03:54,330 --> 00:03:58,530
kind of go back in time and then say, actually update the time stamp here?

55
00:03:58,740 --> 00:04:02,010
Maybe only one admin user to do that from a super user standpoint.

56
00:04:02,040 --> 00:04:06,990
Again, it really depends on the situation, but I do want you to kind of think about those cases where

57
00:04:07,260 --> 00:04:11,460
in this particular case, we're saying you can update any field you want first name, last name or subject

58
00:04:11,460 --> 00:04:12,060
that's being taught.

59
00:04:12,420 --> 00:04:15,510
Maybe in your particular situation, you actually only want to update a few fields.

60
00:04:15,900 --> 00:04:22,380
And in this case, you define the success URL again, notice just how similar this looks to create view.

61
00:04:23,040 --> 00:04:28,830
Now, it's not exactly like create view, because remember when we're updating something, we're only

62
00:04:28,830 --> 00:04:34,770
updating again a single primary key, which means that you URLs is actually going to look a lot more

63
00:04:34,770 --> 00:04:36,960
similar to something like a detailed view.

64
00:04:37,110 --> 00:04:37,950
Let's check that out.

65
00:04:38,380 --> 00:04:46,890
I'm going to go to my URLs that pie and then let's import our teacher update view.

66
00:04:47,750 --> 00:04:50,420
And let's create a new pathway here.

67
00:04:50,990 --> 00:04:54,050
We're going to see path is equal to.

68
00:04:54,080 --> 00:04:57,680
And we're going to call this update teacher forward slash.

69
00:04:57,920 --> 00:05:02,060
And this is where the primary key is going to go into your primary key.

70
00:05:02,840 --> 00:05:07,760
And then we just say teacher update view as a view.

71
00:05:08,120 --> 00:05:11,570
And if you want, you can give this a name as well, like update teacher.

72
00:05:14,450 --> 00:05:16,490
OK, save that change.

73
00:05:16,670 --> 00:05:20,480
And so now it's going to happen, is it going to have the capability to update a teacher?

74
00:05:20,900 --> 00:05:23,230
Now we're should actually link this again.

75
00:05:23,240 --> 00:05:25,160
It kind of depends on what you want to do.

76
00:05:25,460 --> 00:05:30,320
You could if you wanted to create a new template that says, Hey, fill me out for a form to link you

77
00:05:30,320 --> 00:05:34,910
to an update where they just fill out a form, maybe pass in one, two, three four, etc. That's a

78
00:05:34,910 --> 00:05:37,220
primary key and a link to update a teacher.

79
00:05:37,550 --> 00:05:43,580
You can't again really have a generic update teacher link that doesn't really make sense, because remember,

80
00:05:43,610 --> 00:05:45,230
you need a specific primary key.

81
00:05:45,710 --> 00:05:49,940
So maybe a good candidate for that particular link would be similar to where we passed in the teacher

82
00:05:49,940 --> 00:05:50,240
list.

83
00:05:50,600 --> 00:05:53,600
I could say maybe a link here for updating.

84
00:05:54,470 --> 00:05:59,570
So let's create yet another anchor tag, and I'm going to put this all within the same list item.

85
00:06:00,440 --> 00:06:06,650
So we're going to say teacher, first name, last name and then.

86
00:06:07,930 --> 00:06:13,980
This is the list item, but I'm going to create yet another anchor tag here, and it's going to go to

87
00:06:13,990 --> 00:06:14,710
classroom.

88
00:06:16,000 --> 00:06:17,620
Update teacher.

89
00:06:18,700 --> 00:06:21,010
And then it's going to give the teacher ID.

90
00:06:22,150 --> 00:06:25,990
Very similar to a detail to you, except now we're linking them to that update page.

91
00:06:26,350 --> 00:06:31,420
And then here I can just put some text to say update information.

92
00:06:31,660 --> 00:06:35,200
Obviously, you can get a lot more fancy if this maybe this is a button or something like that, but

93
00:06:35,200 --> 00:06:36,220
it's going to save that.

94
00:06:36,580 --> 00:06:40,570
OK, so let's see what our list looks like now.

95
00:06:40,750 --> 00:06:42,040
So I come back to our list.

96
00:06:42,580 --> 00:06:48,190
I refresh this and now we have Adam Smith and then it says update information.

97
00:06:48,250 --> 00:06:50,230
Now I see up the information three times here.

98
00:06:50,500 --> 00:06:51,940
Maybe that's not super clear.

99
00:06:52,150 --> 00:06:56,740
So what I can do is come back here and I also accidentally delete a space there.

100
00:06:57,010 --> 00:06:59,570
And so I can stay up the information for.

101
00:07:00,010 --> 00:07:04,000
And then maybe I say teacher first name.

102
00:07:06,100 --> 00:07:09,640
Something like that again, this is just a team of formatting on what you want to do here.

103
00:07:09,760 --> 00:07:10,870
It's not so much jingo.

104
00:07:11,560 --> 00:07:14,050
So I refresh this and maybe this looks a little nicer.

105
00:07:14,050 --> 00:07:16,060
Arrowsmith Smith up the information for Adam.

106
00:07:16,390 --> 00:07:20,230
Maybe I actually have that as an indented list item, so you could keep kind of messing around with

107
00:07:20,230 --> 00:07:24,010
this to get the best kind of look you're going for.

108
00:07:24,520 --> 00:07:26,350
So I can make this yet another.

109
00:07:27,280 --> 00:07:36,190
Unordered list here and then stick that in as its own list item, so I could copy that passenger list.

110
00:07:37,440 --> 00:07:38,070
Puts that in.

111
00:07:39,720 --> 00:07:45,480
And then let's make sure our tags are close correctly, so this one would go this closing tag.

112
00:07:46,770 --> 00:07:47,460
Would go here.

113
00:07:49,840 --> 00:07:50,320
There we go.

114
00:07:51,230 --> 00:07:52,140
And there we have it.

115
00:07:52,160 --> 00:07:52,430
OK.

116
00:07:52,500 --> 00:07:54,710
Let's see if I hopefully that it must add up.

117
00:07:55,040 --> 00:07:58,610
But essentially it's trying to deal with a nested list and there real Adam Smith update information

118
00:07:58,610 --> 00:08:02,870
for Adam Jose Padilla of the information for the third teacher up the information officer.

119
00:08:03,080 --> 00:08:04,040
OK, not so bad.

120
00:08:04,400 --> 00:08:06,440
Now let's see what happens when I go to Adam Smith.

121
00:08:06,860 --> 00:08:09,790
Remember right now if I click, Adam Smith takes me to the detail view.

122
00:08:09,830 --> 00:08:11,360
Adam Smith teaches economics.

123
00:08:11,810 --> 00:08:15,290
Let's say Adam Smith went ahead and changed the topic.

124
00:08:15,290 --> 00:08:16,760
They were teaching how they want to teach math.

125
00:08:17,150 --> 00:08:18,920
I click on update information for Adam.

126
00:08:19,430 --> 00:08:21,380
And it takes me to that teacher form.

127
00:08:21,740 --> 00:08:27,650
If you look carefully at the URL for this, it says Classroom for update teacher forward slash number

128
00:08:27,650 --> 00:08:28,010
two.

129
00:08:28,370 --> 00:08:30,260
So Adam is the number two teacher.

130
00:08:30,260 --> 00:08:32,390
I ended up creating its primary keys, too.

131
00:08:32,809 --> 00:08:38,809
Then I can come here, it's economics and maybe switch it out to math that I hit submit.

132
00:08:39,740 --> 00:08:42,409
And now we have the list of teachers and the list you.

133
00:08:42,500 --> 00:08:47,480
Adam Smith Jose Padilla, third teacher If you click on the detail for Adam Smith and now says Adam

134
00:08:47,480 --> 00:08:48,770
Smith teaches math.

135
00:08:49,280 --> 00:08:50,840
So what's happening behind the scenes?

136
00:08:51,230 --> 00:08:58,460
Well, when you click on an update view for a particular primary key, it essentially just calls the

137
00:08:58,460 --> 00:08:59,330
teacher form.

138
00:08:59,720 --> 00:09:08,570
But unlike Create View, a update View says Fill it out or pre fill the information that is already

139
00:09:08,570 --> 00:09:11,660
known for this particular entry at this particular primary key.

140
00:09:12,070 --> 00:09:17,810
Then, upon hitting submit, you're going to save those changes an extremely useful view.

141
00:09:18,230 --> 00:09:23,590
Again, all we're doing here is connecting to the same form we had for create view.

142
00:09:23,900 --> 00:09:28,070
Except this time I'm pre filling it with the information for a particular primary key.

143
00:09:28,490 --> 00:09:33,250
That means the same caveats from the details you apply to the update view.

144
00:09:33,290 --> 00:09:35,840
I can only do this for a particular primary key.

145
00:09:36,110 --> 00:09:38,680
I can't just say Hey Link to the update view.

146
00:09:39,170 --> 00:09:43,940
I want to point out what I often see students do as a mistake is on the homepage.

147
00:09:44,210 --> 00:09:51,350
They'll try to make a new link here that says something like anchor tag classroom or URL.

148
00:09:52,520 --> 00:09:54,050
So I'll say something like URL.

149
00:09:55,010 --> 00:09:59,900
Classroom and then they just try to say something like detailed teacher or something like that.

150
00:10:00,110 --> 00:10:06,230
Or maybe they try to say update teacher, but this doesn't really make sense to be on the home page

151
00:10:06,530 --> 00:10:08,620
because I need a primary key.

152
00:10:08,630 --> 00:10:12,740
And if I just have this is a link, there's no information of what teacher actually want to update.

153
00:10:13,160 --> 00:10:18,080
So if you want to make a generic link like that, that would have to first take you to some sort of

154
00:10:18,080 --> 00:10:20,640
form where the client can inform you.

155
00:10:20,660 --> 00:10:26,150
I want to update user number one or user number two, user number three, etc., which is why typically

156
00:10:26,150 --> 00:10:33,260
it's kind of nicer to combine the idea of this list view with the fact that I know that it is already

157
00:10:33,260 --> 00:10:37,370
there for me, rather than have the user maybe provide accidentally the wrong idea.

158
00:10:37,430 --> 00:10:40,700
Of course, it's up to you and the website you're creating, but you get the idea.

159
00:10:41,060 --> 00:10:41,330
All right.

160
00:10:41,360 --> 00:10:45,290
Hopefully, you see now how powerful these class based views were.

161
00:10:45,590 --> 00:10:49,920
And we're going to discover one more class based view, which is the delete view.

162
00:10:49,940 --> 00:10:51,590
And it's essentially like update.

163
00:10:51,740 --> 00:10:55,550
Except this time, instead of updating, it's going to delete that particular entry.

164
00:10:55,940 --> 00:10:57,380
Let's check it out in the next lecture.

