text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
So I'll go ahead and say user is equal to authenticate request username is
6,537.06
5.18
the username password equals password.
6,542.24
3.04
And so authenticate is a function.
6,545.28
1.55
Just takes the request, takes a user name, takes a password,
6,546.83
3.57
and if the user name and password are valid,
6,550.4
2.13
they give me back who the user actually is.
6,552.53
3.61
And as long as the user is not none, that
6,556.14
4.31
means the authentication was successful, and I can go ahead and log the user in.
6,560.45
4.11
How do I log the user in?
6,564.56
1.5
I use the log in function the Django gives me.
6,566.06
2.43
Logging in with this request, this user.
6,568.49
3.43
And now I can go ahead and redirect them.
6,571.92
2.42
HttpResponseRedirect the user back to the index route.
6,574.34
4.2
Back to the original route that the user started out as.
6,578.54
2.767
And so that's if the user is not none.
6,581.307
1.583
If the authentication was successful.
6,582.89
2.04
But otherwise, if the authentication failed, what should I do?
6,584.93
3.69
Well, let me go ahead and render the same users login page again.
6,588.62
5.13
But let me add some additional context.
6,593.75
2.52
The context will be a message that says invalid credentials.
6,596.27
6.13
And now, inside of login.html, I can just
6,602.4
2.66
add some logic that says, if there's a message,
6,605.06
3.39
then go ahead and display that message inside of a div.
6,608.45
4.17
And then endif to end that.
6,612.62
1.47
So if there is a message, we'll see the message printed.
6,614.09
2.46
Otherwise, we won't see it at all.
6,616.55
2.86
So now, if I go ahead and refresh the login page,
6,619.41
2.66
nothing seems to have changed.
6,622.07
1.47
But let's say I type in a user name that doesn't exist.
6,623.54
2.36
Hermione and some password and I log in.
6,625.9
2.98
Well, then I get this error message.
6,628.88
1.5
Invalid credentials.
6,630.38
1.11
We were not able to log the user in.
6,631.49
2.35
So what happens if we do successfully logged in?
6,633.84
2.45
Well, then the user is going to be taken to this index route.
6,636.29
4.178
And it looks like now we need to finish off this index route.
6,640.468
2.542
What does the index route do?
6,643.01
1.53
Well, let's go ahead and return render a template called users/user.html.
6,644.54
4.62
And inside of user.html, we'll go ahead and display
6,652.28
4.7
some information about the user.
6,656.98
2.55
We'll still extend user/layout because we're
6,659.53
2.7
going to use the same basic layout.
6,662.23
2.13
But in the body of this page, the information I want to show--
6,664.36
3.86
if I want to say welcome and then like Harry or welcome Ron or whoever
6,668.22
4.84
the user happens to be.
6,673.06
1.99
And it turns out inside of Django templates,
6,675.05
2.39
I have access to the request that was used to make this HTTP request, which
6,677.44
4.44
means I also have access to request.user who is the user associated
6,681.88
4.41
with that request.
6,686.29
1.29
And if the user has a first name, I can access request.user.firstname.
6,687.58
5.965
And in addition to that, I can display other information.
6,693.545
2.375
Maybe their user name is request.user.username.
6,695.92
3.78
And maybe their email address is request.user.email.
6,699.7
4.23
And so I can show the user information about them.
6,703.93
3.72
Such that if Harry logs in, for example, I sign in as Harry,
6,707.65
3.84
sign in with Harry's credentials, click log in.
6,711.49
4.56
Well then, Harry sees a page that says welcome Harry.
6,716.05
2.91
Harry is logged in.
6,718.96
1.08
They are request.user.
6,720.04
1.98
And using that information, we can access first name, username, and email
6,722.02
5.13
as well just by accessing properties of request.user.
6,727.15
4.8
Now, last thing we need to add, which still doesn't yet exist,
6,731.95
3.06
is a way to actually log the user out.
6,735.01
2.5
And it turns out that just as Django has a log in function,
6,737.51
3.86
Django also has a log out function that handles log out for us so we
6,741.37
3.24
don't need to implement it ourselves.
6,744.61
1.83
So all our log out view needs to do is make a call to this log out function.
6,746.44
5.25
And then, figure out where should the user go after they've been logged out.
6,751.69
3.21
And you know what, let's go ahead and take them back to the login page
6,754.9
5.5
with a message of logged out to indicate that the user has now been logged out.
6,760.4
6
Then in user.html, we'll go ahead and add a link that will go to the log
6,766.4
7.89
out route that just says, log out, for example.
6,774.29
5.61
So now, when Harry goes back to Harry's page,
6,779.9
2.06
Harry sees a URL that says log out.
6,781.96
2.19
If Harry clicks log out, Harry gets logged out is brought back to this page
6,784.15
4.23
because now request.user.isauthenticated is going to be false.
6,788.38
4.15
There is no authenticated user.
6,792.53
1.88
And so they now see just the default login page.
6,794.41
3.06
And if now Ron were to log in, for example, using Ron's username
6,797.47
3.84
and Ron's password logging in, then Ron now
6,801.31
3.39
sees information associated with him as well.
6,804.7
3.27
So Django gives us a lot out of the box.
6,807.97
2.04
Gives us the ability to represent these models in admin interface,
6,810.01
3.18
to be able to manipulate them.
6,813.19
1.35
A migration system that allows us to very quickly make changes to our models
6,814.54
3.93
and apply them to our database.
6,818.47
1.59
And also, a built in user authentication system.
6,820.06
2.46
A system that allows us to very quickly enable
6,822.52
2.61
users to be able to log in, log out, from our web application as well.
6,825.13
4.268
So all of this are features that just helps
6,829.398
1.792
to make it so that we can very quickly take advantage of things like SQL
6,831.19
3.57
and models and migrations to build dynamic, interesting web
6,834.76
3.18
applications with data to back them up.
6,837.94
2.67
This was web programming with Python and JavaScript.
6,840.61
2.28
We will see you next time.
6,842.89
2.27
[MUSIC PLAYING]
0
0
>> DAVID J. MALAN: All right, this is CS50.
13.95
2.29
And this is week one.
16.24
1.77
So recall that last time in week zero, we focused on computational thinking.
18.01
4.04
And we transitioned from that to Scratch, a graphical programming
22.05
3.39