1
00:00:05,320 --> 00:00:06,280
Welcome back, everyone.

2
00:00:06,310 --> 00:00:09,580
In this lecture, we're going to go through some user authentication forms.

3
00:00:09,760 --> 00:00:16,420
Well, if you want, you could use the built in user object to create users manually using your own

4
00:00:16,420 --> 00:00:16,810
form.

5
00:00:17,170 --> 00:00:23,770
Luckily for us, Django provides quite a few built in forms and views based off the user class that's

6
00:00:23,770 --> 00:00:24,610
built in the Django.

7
00:00:24,970 --> 00:00:27,580
Let's explore some of these in the documentation and in our code.

8
00:00:28,180 --> 00:00:28,480
Okay.

9
00:00:28,480 --> 00:00:32,439
So here I am inside the documentation for using the Django authentication system.

10
00:00:32,710 --> 00:00:37,180
By this point in time, we should understand enough to be able to read this documentation page.

11
00:00:37,450 --> 00:00:42,400
We didn't jump straight to it at the beginning of this section because there are a few concepts that

12
00:00:42,610 --> 00:00:46,990
the documentation kind of assumes you already know before you actually start reading it.

13
00:00:47,200 --> 00:00:53,470
So I should point out that everything we've been using has fallen under the built in user objects,

14
00:00:53,470 --> 00:00:56,620
which, as we already know, are built into the authentication system.

15
00:00:56,620 --> 00:00:58,450
And we've seen them inside the admin.

16
00:00:58,810 --> 00:01:04,120
And we also already know that the primary attributes of a user are username, password, email, first

17
00:01:04,120 --> 00:01:05,170
name and last name.

18
00:01:05,230 --> 00:01:07,720
I should point out that there's full API documentation.

19
00:01:08,080 --> 00:01:14,140
So if you want, you could continue to customize user objects by actually inheriting the user class

20
00:01:14,140 --> 00:01:15,700
and then adding more fields to it.

21
00:01:16,000 --> 00:01:20,710
If you needed something like let's say a profile picture for your particular website, you could start

22
00:01:20,710 --> 00:01:22,780
linking that inside the user object.

23
00:01:22,870 --> 00:01:28,870
Or probably more appropriately, could we do something like a profile picture model that we link through

24
00:01:28,870 --> 00:01:33,460
a foreign key to a user, lots of different ways to actually tackle that sort of problem.

25
00:01:34,090 --> 00:01:37,030
Then as I mentioned, you can create users directly.

26
00:01:37,150 --> 00:01:42,520
So the way you could do this is you could say from Django that can trim the off but models import user

27
00:01:42,940 --> 00:01:47,170
and just with this one line especially it means that you have full access to the user class.

28
00:01:47,530 --> 00:01:53,080
So everything we learned about creating forms or creating instances inside a model class for adding

29
00:01:53,080 --> 00:01:56,350
a new row to a database that can all be done for the user as well.

30
00:01:56,770 --> 00:01:58,060
You can see it's just user.

31
00:01:58,060 --> 00:02:03,520
The objects that create user and all that knowledge that you know already can be done automatically

32
00:02:03,520 --> 00:02:05,620
for you using the user class.

33
00:02:05,620 --> 00:02:09,729
So this allows you to do a lot of things the way you already know how.

34
00:02:10,060 --> 00:02:13,480
You're just swapping out your typical model for a user model.

35
00:02:14,290 --> 00:02:17,560
I should point out you can also do things like change passwords.

36
00:02:17,590 --> 00:02:23,050
So you can see here you could user the objects that get a particular user or a username and then set

37
00:02:23,050 --> 00:02:24,310
their password to a new password.

38
00:02:24,640 --> 00:02:29,350
This is a very manual way of doing this, but later on I'll show you that there's actually views to

39
00:02:29,410 --> 00:02:30,700
tackle some of those issues.

40
00:02:30,700 --> 00:02:34,030
And as you mentioned, there's also things like authenticating users.

41
00:02:34,150 --> 00:02:40,270
So if you want, instead of doing authentication on the template side of things with user ID is authenticated,

42
00:02:40,600 --> 00:02:43,600
you can actually do it on the python end of things.

43
00:02:43,600 --> 00:02:49,060
So you can say user authenticate and then you could grab the username and password from something being

44
00:02:49,060 --> 00:02:50,890
provided based on the view.

45
00:02:50,950 --> 00:02:54,490
So you can use a lot of this code inside of use.

46
00:02:54,580 --> 00:02:59,080
There's also things like permissions and authorizations, things that we've kind of talked about, but

47
00:02:59,080 --> 00:03:00,370
now we can see it here in code.

48
00:03:00,700 --> 00:03:06,700
For example, we can see my user groups and then you can set, add or remove, clear, etc., which

49
00:03:06,700 --> 00:03:09,520
means you could actually grab my user ID doc groups.

50
00:03:09,910 --> 00:03:14,710
It would return a list of all the groups that the user belongs to and you could start filtering things

51
00:03:14,710 --> 00:03:15,940
based off that group.

52
00:03:16,060 --> 00:03:21,880
So for example, you could say if the user belongs to the librarian group, essentially saying, if

53
00:03:21,880 --> 00:03:26,860
librarian is in my user groups, then you could check, okay, show them the book.

54
00:03:27,190 --> 00:03:28,900
If not, maybe you show them something else.

55
00:03:28,900 --> 00:03:33,890
So there's a lot of functionality here that you just need to understand everything we've done previously

56
00:03:33,890 --> 00:03:39,040
to then leverage the tools that the user built in model already provides for you.

57
00:03:39,940 --> 00:03:44,860
Now that's a way of doing a lot of things manually, but luckily for us, there's also a bunch of what

58
00:03:44,860 --> 00:03:48,370
are known as authentication views as well as built in forms.

59
00:03:48,460 --> 00:03:53,410
So there's things that we've talked about like logging require a decorator and logging required mixing,

60
00:03:53,410 --> 00:03:55,750
etc. A lot of that is manual.

61
00:03:55,780 --> 00:04:00,010
But I should point out if you click here on authentication views, so click on that.

62
00:04:00,610 --> 00:04:05,710
There's the views that we already discussed, the log in, log out, but there's also views for doing

63
00:04:05,710 --> 00:04:11,350
things like a password change or a password reset or just resetting the account itself.

64
00:04:11,470 --> 00:04:16,390
So there's lots of things you can do here that are already built in for you, and all you need to do

65
00:04:16,390 --> 00:04:22,570
is import them from Django that contribute off import views as and they have off views.

66
00:04:22,900 --> 00:04:25,990
So you have all these views available for you as class based views.

67
00:04:26,380 --> 00:04:28,210
So keep in mind again, they're all class based.

68
00:04:28,510 --> 00:04:33,310
Those means you can customize them with subclasses and then there's a bunch of authentication views.

69
00:04:33,310 --> 00:04:38,500
So there's a log in view, there's a log out view, a password change view, and you can see some examples

70
00:04:38,500 --> 00:04:43,180
here of how they're actually using this very similar to everything we've done previously.

71
00:04:43,450 --> 00:04:47,530
Now, the other thing I want to point out is beyond the views that are built in.

72
00:04:47,770 --> 00:04:52,060
So I would encourage you to check these out when you need something like a quick password reset view.

73
00:04:52,720 --> 00:04:58,300
There are the built in forms, so there's a bunch of forms that you could set up inside a forms that

74
00:04:58,360 --> 00:05:00,550
profile and then import into your views.

75
00:05:00,910 --> 00:05:04,690
Or if you want, you can kind of skip the step of setting up as a separate forms up.

76
00:05:04,770 --> 00:05:06,870
I file and use them directly, in your view?

77
00:05:07,410 --> 00:05:12,060
If you're wondering how to use these built in forms, you basically use them in the exact same manner

78
00:05:12,060 --> 00:05:14,370
that we discussed when using Django forms.

79
00:05:14,460 --> 00:05:16,380
These are just filled out forms for you.

80
00:05:16,860 --> 00:05:23,290
For example, an admin password change form or an authentication form or a password reset form, etc..

81
00:05:23,310 --> 00:05:28,230
So let's go through an example of using one of these, which is the user creation form.

82
00:05:28,410 --> 00:05:34,230
So right now, an administrator needs to log into the admin part of our website in order to create a

83
00:05:34,230 --> 00:05:34,890
new user.

84
00:05:35,280 --> 00:05:40,260
But obviously, creating a new user should probably be something that's simple enough to set it up in

85
00:05:40,260 --> 00:05:42,990
a form that we can actually set up in a view.

86
00:05:43,380 --> 00:05:46,000
And of course, there's going to be a form for that.

87
00:05:46,010 --> 00:05:51,300
So there's a class user creation form, which is just a model form for creating a new user.

88
00:05:51,780 --> 00:05:55,500
So as you might have expected, it's relying on all that knowledge that we've already learned about.

89
00:05:55,830 --> 00:05:58,850
We already know about forms and we already know about model forms.

90
00:05:58,860 --> 00:06:04,530
And we know that user is just a special model that's already built into Django, which means a user

91
00:06:04,530 --> 00:06:07,770
creation form is just a model form for that user class.

92
00:06:08,250 --> 00:06:10,350
Let's actually explore this in our code.

93
00:06:10,800 --> 00:06:16,530
Okay, so here I am back inside it views that py where previously we added in log in required as decorator

94
00:06:16,830 --> 00:06:18,140
and log in required mix.

95
00:06:18,150 --> 00:06:19,770
And we also have our index page.

96
00:06:20,070 --> 00:06:24,360
Remember, we did a lot of work on the template side of things to check if someone is logged in or logged

97
00:06:24,360 --> 00:06:24,660
out.

98
00:06:25,110 --> 00:06:30,590
And we can also see we have some May book create and book detailed views as well as this random view

99
00:06:30,600 --> 00:06:33,030
to show you how to add in log in required.

100
00:06:33,510 --> 00:06:38,250
What I want to do now is actually set up a view where we can sign up a new user.

101
00:06:38,850 --> 00:06:43,020
It's kind of up to you how you want to set this up, if it should be under registration, for example,

102
00:06:43,020 --> 00:06:45,300
or maybe you want it specific to catalog.

103
00:06:45,720 --> 00:06:48,420
We'll show you how to set it up on your own custom path.

104
00:06:48,990 --> 00:06:51,780
And what we're going to do is set up a sign up template.

105
00:06:51,990 --> 00:06:54,390
So first off, let's actually create the view.

106
00:06:54,390 --> 00:07:03,450
And as I mentioned, we can just grab the form itself by saying from Django, dot, contrib, dot off

107
00:07:03,510 --> 00:07:10,140
the forms, import a user and we can see here there's a user creation form.

108
00:07:10,170 --> 00:07:11,970
There's also things like a user change form.

109
00:07:11,970 --> 00:07:16,320
So if you want to change information about an existing user and there's a bunch of those built in forms

110
00:07:16,320 --> 00:07:17,640
like change, password, etc..

111
00:07:18,150 --> 00:07:19,800
So there's also a bunch of views.

112
00:07:19,800 --> 00:07:26,250
And remember, this is technically a form, it's not a view, so it's a model form for the user class.

113
00:07:26,640 --> 00:07:28,020
Careful not to confuse those two.

114
00:07:28,050 --> 00:07:32,400
Remember, the views are already set to go inside of used up by you.

115
00:07:32,400 --> 00:07:36,990
Just use it like a detail view or a create view or model form based view.

116
00:07:37,500 --> 00:07:43,020
And here we're actually just dealing with the form itself, which means we actually plug that in using

117
00:07:43,020 --> 00:07:45,750
the form class attribute into a class based view.

118
00:07:46,380 --> 00:07:50,490
So what's the best way and easiest way to use user creation form?

119
00:07:51,120 --> 00:07:57,060
If I wanted to create a new user, I could manually create a little function based model form setup

120
00:07:57,060 --> 00:08:02,070
thing that says, okay, user, the object that create new user, etc. and do it all manually.

121
00:08:02,280 --> 00:08:06,000
But the user creation form can save a lot of time if we don't want to do it all manually.

122
00:08:06,360 --> 00:08:09,000
And I can do it by piggybacking off create view.

123
00:08:09,150 --> 00:08:10,260
So let me show you how that's done.

124
00:08:11,130 --> 00:08:11,790
I'm going to.

125
00:08:12,940 --> 00:08:14,850
Create a new class here.

126
00:08:14,860 --> 00:08:18,730
So is the class based view and I'm going to call it my sign up view.

127
00:08:19,330 --> 00:08:20,710
So we're going to call it sign up.

128
00:08:21,620 --> 00:08:25,130
View and we intend to sign up people using this class.

129
00:08:25,550 --> 00:08:28,520
And I'm actually going to inherit from creative you.

130
00:08:30,200 --> 00:08:32,360
So it's a sign up view where we actually create.

131
00:08:32,570 --> 00:08:39,799
And because I'm using create view, that means I can use the built in form class attribute and overwrite

132
00:08:39,799 --> 00:08:42,520
it with the user creation form.

133
00:08:43,549 --> 00:08:48,860
And then what I can do here is simply do what we already know, which is do things like set up a successful

134
00:08:48,860 --> 00:08:49,220
URL.

135
00:08:49,700 --> 00:08:55,040
So after they're actually create a user, I'm going to have them log in so I can do something like a

136
00:08:55,040 --> 00:08:59,140
reverse lazy lookup for the login view.

137
00:08:59,390 --> 00:09:05,400
So we can say log in here, but we have to make sure these names are starting to line up.

138
00:09:05,420 --> 00:09:07,100
So this is actually based in catalog.

139
00:09:07,490 --> 00:09:09,920
So let's check this out inside the URLs that py.

140
00:09:10,340 --> 00:09:15,800
So I don't actually have anything for a log in view right now, so I need to be aware of the fact that

141
00:09:16,310 --> 00:09:19,340
I may have issues if I actually want to link that using your URLs.

142
00:09:20,420 --> 00:09:25,340
You'll see later on this actually will be okay for us because if you take a look at, for example,

143
00:09:25,340 --> 00:09:27,020
index view, it's actually all built in.

144
00:09:27,030 --> 00:09:33,170
You can see that I'm requesting your URL login so that naming convention by default because it's actually

145
00:09:33,170 --> 00:09:38,180
built into Django at the top level, I don't actually have to worry about being able to look up reverse

146
00:09:38,180 --> 00:09:39,770
lazy, so I'm going to save that.

147
00:09:39,980 --> 00:09:45,050
And then the last thing going to do here is set up a template name to send back that form so you can

148
00:09:45,050 --> 00:09:46,040
say template name.

149
00:09:50,210 --> 00:09:51,200
Is equal to.

150
00:09:51,200 --> 00:09:53,270
And if you want, you can actually build this inside of catalog.

151
00:09:53,330 --> 00:09:54,020
It's kind of up to you.

152
00:09:56,130 --> 00:09:58,980
Some people like putting an inside registration, which could also make sense.

153
00:09:58,980 --> 00:10:02,520
But I just want you to keep in mind, you can basically pass it in wherever you want.

154
00:10:02,520 --> 00:10:04,080
So signed up page HTML doesn't exist yet.

155
00:10:04,470 --> 00:10:06,060
Let's go ahead and put that in.

156
00:10:06,060 --> 00:10:08,280
But first, let's connect this view to yours.

157
00:10:08,280 --> 00:10:08,970
I'll stop by.

158
00:10:08,970 --> 00:10:12,900
So I'm going to come back to URLs, that pie and let's put in that sign up view.

159
00:10:12,900 --> 00:10:17,940
So I'm gonna say path and let's have it be sign up for it slash.

160
00:10:18,980 --> 00:10:24,260
And then we're going to say views dart sign up view and remember it's a class based view.

161
00:10:24,260 --> 00:10:27,290
So I need a call as view and let's give it the name.

162
00:10:28,440 --> 00:10:28,920
Sign up.

163
00:10:30,670 --> 00:10:31,000
Okay.

164
00:10:31,150 --> 00:10:36,550
So so far, while it may appear we're doing stuff that's super new to us, we're really just operating

165
00:10:36,550 --> 00:10:38,860
on principles that we've already discussed before.

166
00:10:39,340 --> 00:10:42,150
All we're doing here is using the create view.

167
00:10:42,160 --> 00:10:47,230
It's a class based view that we already familiar with and we're just overwriting the form with the default

168
00:10:47,230 --> 00:10:48,340
user creation form.

169
00:10:48,700 --> 00:10:50,170
That saves us a lot of that work.

170
00:10:50,320 --> 00:10:55,870
I remember because it's a class based view, it's essentially going to automatically pass in that form

171
00:10:55,870 --> 00:11:01,210
for us and we can set up a successor are also once they register, takes them to that login page and

172
00:11:01,210 --> 00:11:04,330
then we can say a template name catalog sign up three simple.

173
00:11:04,330 --> 00:11:05,230
So let's set up that template.

174
00:11:05,620 --> 00:11:07,390
So inside catalog, I'll create a new file.

175
00:11:08,320 --> 00:11:09,460
And I'll call it sign up.

176
00:11:09,700 --> 00:11:10,240
It's HTML.

177
00:11:11,450 --> 00:11:11,990
There we go.

178
00:11:12,530 --> 00:11:15,230
Now, remember, this is still a form, so I have to do a little bit of work here.

179
00:11:15,500 --> 00:11:20,060
So let's go ahead and say H1 register new user here.

180
00:11:22,590 --> 00:11:24,690
And then we'll say, create a form.

181
00:11:26,510 --> 00:11:28,070
And then we'll see Methodists post.

182
00:11:28,480 --> 00:11:30,920
So say method is equal to.

183
00:11:32,090 --> 00:11:32,630
Post.

184
00:11:32,840 --> 00:11:37,130
So we have our form and then we're just going to create a CSR token.

185
00:11:38,420 --> 00:11:41,180
And then I'm going to pass in the form itself.

186
00:11:42,220 --> 00:11:45,010
So it was not what I want to hear actually once.

187
00:11:46,300 --> 00:11:46,840
Form.

188
00:11:46,870 --> 00:11:53,170
And then I can say that as Pete and that with paragraph tags for each field, remember it's the username,

189
00:11:53,170 --> 00:11:59,110
the email password, etc. and then finally I need a button here or I can also do an input.

190
00:11:59,300 --> 00:11:59,980
Doesn't really matter.

191
00:12:00,400 --> 00:12:01,660
So I can say input type.

192
00:12:02,860 --> 00:12:04,810
And what I need is submit.

193
00:12:05,940 --> 00:12:09,450
And then the value will be let's just say that's a classic sign up.

194
00:12:09,450 --> 00:12:10,650
So it's clear what we're doing.

195
00:12:14,040 --> 00:12:16,710
Um, and let's have this be one word here.

196
00:12:18,560 --> 00:12:19,190
Sign up.

197
00:12:21,600 --> 00:12:23,280
And there we have it.

198
00:12:23,790 --> 00:12:24,090
Okay.

199
00:12:26,650 --> 00:12:29,360
So we have that form set up for us views.

200
00:12:29,380 --> 00:12:30,580
I'm going to say that right now.

201
00:12:31,240 --> 00:12:33,930
And have we actually imported reverse lazy yet?

202
00:12:34,100 --> 00:12:35,170
It looks like it's not the final.

203
00:12:35,170 --> 00:12:35,950
Let's actually do that.

204
00:12:36,520 --> 00:12:42,250
So we'll say from Django that your URLs imports and we should see now reverse lazy.

205
00:12:43,060 --> 00:12:43,720
Okay there we go.

206
00:12:44,170 --> 00:12:44,980
So nice of us.

207
00:12:44,980 --> 00:12:46,570
That voice code was telling us to do that.

208
00:12:46,960 --> 00:12:52,360
So what I've done so far is I imported the user creation form, set up this new class based view and

209
00:12:52,360 --> 00:12:57,130
I'm thinking it to sign up, which should automatically link to the model for user.

210
00:12:57,130 --> 00:12:59,020
So nothing too crazy that's going on.

211
00:12:59,380 --> 00:13:03,520
I will have to manually type out the sign up link, but let's go ahead and check it out.

212
00:13:04,000 --> 00:13:07,300
So in your URLs, I'm not going to go to catalog for slash sign up.

213
00:13:08,080 --> 00:13:09,970
So let's go to the main page.

214
00:13:09,970 --> 00:13:11,590
Let's also make sure we're not logged in.

215
00:13:12,400 --> 00:13:14,550
So here I am, I'm already logged in.

216
00:13:14,560 --> 00:13:15,310
We're going to log out.

217
00:13:15,640 --> 00:13:20,620
So I'm no longer logged in and then I'm going to go to forward slash sign up.

218
00:13:21,280 --> 00:13:23,320
This takes me to registering new user here.

219
00:13:23,320 --> 00:13:28,690
You'll notice all these form stuff or form fields that's automatically done for you with the user creation

220
00:13:28,690 --> 00:13:29,020
form.

221
00:13:29,440 --> 00:13:31,360
I should note that there's a bunch of limitations.

222
00:13:31,360 --> 00:13:33,490
Your password has to can't be too similar.

223
00:13:33,940 --> 00:13:36,370
Um, must contain at least eight characters, etc..

224
00:13:36,400 --> 00:13:41,470
So if you're getting a failure upon creating a new user, make sure your password is actually okay.

225
00:13:42,160 --> 00:13:43,810
So let's create a username here.

226
00:13:43,810 --> 00:13:50,560
I'm going to say my user two and the password will be let's say my password.

227
00:13:51,670 --> 00:13:53,090
One, two, three.

228
00:13:53,110 --> 00:13:54,250
Hopefully that's okay.

229
00:13:54,280 --> 00:13:55,240
I think that should be okay.

230
00:13:55,630 --> 00:13:57,520
You do have this automatic password confirmation.

231
00:13:57,520 --> 00:13:58,210
So my.

232
00:13:59,540 --> 00:14:03,510
Password 1 to 3 and let's try it out.

233
00:14:03,530 --> 00:14:04,490
I'm going to click sign up.

234
00:14:05,570 --> 00:14:08,450
And now I'm taking to the login page you can see by the URL.

235
00:14:09,050 --> 00:14:11,690
So now I'm going to say my user to.

236
00:14:12,770 --> 00:14:14,150
My password.

237
00:14:15,050 --> 00:14:16,280
One, two, three.

238
00:14:17,680 --> 00:14:18,340
Log in.

239
00:14:19,460 --> 00:14:21,000
And welcome my user, too.

240
00:14:21,350 --> 00:14:21,650
All right.

241
00:14:21,920 --> 00:14:23,390
So pretty easy to do.

242
00:14:23,750 --> 00:14:28,400
Basically, we leveraged all the ideas that we already understand and learned about in order to register

243
00:14:28,400 --> 00:14:29,140
new users.

244
00:14:29,150 --> 00:14:31,100
And there were many ways we could have done this.

245
00:14:31,130 --> 00:14:36,180
I want to point out very specifically that this was not the only way we could have done this.

246
00:14:36,200 --> 00:14:40,700
We could have manually set up a form based off what we already know how to do.

247
00:14:40,730 --> 00:14:48,260
So if you really wanted to, you could review our lectures on Django on forms, and then you could have

248
00:14:48,260 --> 00:14:51,110
come down to the topics of user authentication.

249
00:14:51,650 --> 00:14:54,260
And then if you came here, could have gone to overview.

250
00:14:54,410 --> 00:14:59,510
And eventually we can go to that page where it says, working with user objects and you could have done

251
00:14:59,510 --> 00:15:00,290
this manually.

252
00:15:00,590 --> 00:15:06,770
So you already know how to use Django to create instances of models manually by saying, okay, grab

253
00:15:06,770 --> 00:15:10,430
the information from a custom form I created and then create that user.

254
00:15:10,790 --> 00:15:15,920
Obviously this is such a common request that the user creation form object has already been created

255
00:15:15,920 --> 00:15:17,270
for you by Django.

256
00:15:17,570 --> 00:15:23,000
But I just want to point that out that you technically, just by viewing this box of code here, you

257
00:15:23,000 --> 00:15:25,940
already know how to use all the skills to create a user.

258
00:15:26,240 --> 00:15:30,560
It's just you have to remember that all you have to do is import user and then you can do that itself.

259
00:15:30,950 --> 00:15:33,140
Way easier to use the user creation form though.

260
00:15:33,500 --> 00:15:35,090
Okay, so that's it.

261
00:15:35,390 --> 00:15:41,150
On how to let the user create users or register from their side of things instead of having to do it

262
00:15:41,150 --> 00:15:41,930
all through the admin.

263
00:15:42,470 --> 00:15:43,910
Thanks and I'll see you at the next lecture.

