text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
And in order to make this work, I'm going
6,233.142
1.708
to need to-- from django.http import HttpResponseRedirect.
6,234.85
5.46
And likewise, from django.urls, let's go ahead and import reverse as well.
6,240.31
6.16
So if the user is not authenticated, then
6,246.47
2.45
we're going to redirect them to the log in view, where what is the log in view
6,248.92
3.63
going to do?
6,252.55
1.42
Well, the log in view for now, let's just go ahead and render
6,253.97
4.97
users/login.html.
6,258.94
2.73
Some form where the user can log themselves in.
6,261.67
3.11
We'll need to create some templates.
6,264.78
1.57
I'll create a templates folder inside of which is a user's folder.
6,266.35
4.14
Inside of which we'll just create a basic layout,
6,270.49
2.4
as we've done multiple times now.
6,272.89
3.31
This is, again, going to be the general structure for pages in this app.
6,276.2
3.81
Title will be users, and the body will just
6,280.01
2.79
have a block called body that I can later fill in with other content.
6,282.8
5.61
And now that I have this HTML layout, I can go ahead and create a new file
6,288.41
3.93
called login.html, where login.html will extend user/layout.html.
6,292.34
7.73
And inside the body block, I can just display in HTML form.
6,300.07
6.552
So I can say something like, I would like
6,306.622
1.708
for there to be a form, whose action, when I submit the forum--
6,308.33
3.54
let's go ahead and still go to the log in URL,
6,311.87
3
but let's do so using the post request method.
6,314.87
3.09
Again, I'm logging in, I'm submitting a form.
6,317.96
2.16
Generally, when you're doing that, you want
6,320.12
1.792
to submit form data via post, especially in the case of user name and password.
6,321.912
3.458
Because if you do this sort of thing, you
6,325.37
1.842
don't want the user name and password to be passed in as get parameters
6,327.212
2.958
because those show up in the URL.
6,330.17
2.43
Our form will have our CSRF token for security as before.
6,332.6
3.93
And input whose type is text, whose name is username, and just
6,336.53
4.56
for user friendliness, let's give it a placeholder also of user name
6,341.09
3.15
so the user knows to type in their user name here.
6,344.24
3.12
We'll also have an input whose type is password, whose name is also password,
6,347.36
5.11
and when an input's type is password, that just
6,352.47
2.15
means our HTML will know in the browser that chrome or Safari or whatnot
6,354.62
3.57
will know to show the password as dots instead of as characters.
6,358.19
4.02
And we'll give that a placeholder of password.
6,362.21
3.48
And then an input of type submit whose value is log in.
6,365.69
4.32
So we now have the ability to log in.
6,370.01
3.39
So if we go ahead and run this program, python manage.py run server,
6,373.4
3.96
we should see users.views has no attribute log in view.
6,377.36
4.44
All right, it looks like I called this function log in request.
6,381.8
3.45
It should actually be called log in view.
6,385.25
1.89
And I'll also need a function called log out view.
6,387.14
2.94
But I haven't implemented that yet.
6,390.08
1.86
So I'll just go ahead and say pass for now,
6,391.94
1.91
but I'll come back to that later to implement the log out view.
6,393.85
3.43
All right, so it looks like my web server is running now.
6,397.28
2.52
And before I actually go to the login page,
6,399.8
2.16
let me first go back to the admin page and actually just create some users.
6,401.96
3.84
I can go to users and then add, and let's add a user.
6,405.8
3.34
The user name will be Harry, for example.
6,409.14
2.63
And we'll go ahead and give Harry a password.
6,411.77
2.697
And we'll go ahead and save and add another.
6,414.467
1.833
Let's add maybe Ron as well.
6,416.3
3.56
Go ahead and add him.
6,419.86
1.45
We'll go ahead and save that.
6,421.31
1.262
And these users, they can have additional information
6,422.572
2.208
associated with them.
6,424.78
0.875
I can give Ron a name like Ron Weasley, RonWeasley@example.com
6,425.655
3.75
is his email address.
6,429.405
1.345
There are a bunch of default fields that Django gives you
6,430.75
2.67
for manipulating users.
6,433.42
1.312
And you can take these users and go ahead and add to those fields
6,434.732
2.708
if I want to.
6,437.44
0.57
Giving them a first name, last name, email address, and whatnot.
6,438.01
3.68
And you can also customize these fields as well.
6,441.69
2
If you'd like to add custom fields that you would like to keep track of with
6,443.69
3.29
regards to your individual users.
6,446.98
2.61
And I will go ahead and log out from Django admin
6,449.59
2.13
now because I don't need it anymore.
6,451.72
2.16
But now, if I go to /users, I'm not authenticated.
6,453.88
4.435
So what I see is a log in form that just looks like this.
6,458.315
2.375
A place for me to type in a user name and a password.
6,460.69
3.33
And of course, I could type in Harry's username and password.
6,464.02
2.55
But I haven't yet implemented the processing of that data yet.
6,466.57
3.27
So let's go ahead and do that now.
6,469.84
2.44
We'll go ahead and go back to views.py.
6,472.28
2.99
In the log in view, there are two ways the log in view function
6,475.27
2.663
could be called.
6,477.933
0.667
One is via the get request method, meaning just show me the log in form.
6,478.6
3.66
And one is via post, submit data to the log in form as well.
6,482.26
3.58
So if the request method is post, well then,
6,485.84
4.73
let me first get the user name which will be inside of the post
6,490.57
3.06
data in a field called user name.
6,493.63
2.1
And let me get the password, which will be
6,495.73
1.98
in a field in the request.post inside of password.
6,497.71
5
And now what I'd like to do is try to authenticate this user.
6,502.71
3.94
And how do I go about doing that?
6,506.65
1.52
Well, it turns out there are a couple of functions that Django
6,508.17
2
has given to me that I can import.
6,510.17
1.76
So from django.contrib.auth, auth for authentication.
6,511.93
4.27
I'm going to import three functions we're ultimately going to use.
6,516.2
2.79
One is authenticate that checks if user name and password are correct.
6,518.99
3.67
One is called log in, one is called log out.
6,522.66
4.75
And I can now use those functions inside of this log in view here.
6,527.41
3.52
After I've gotten the username and password,
6,530.93
1.95
I'd like to authenticate the user.
6,532.88
1.53
Check if the user name and password are correct.
6,534.41
2.65