Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
If we're going to let users log in, we should let them log out, too. And before there can be any logging in or out, users have to be able to sign up. Let's see how to build these very important and useful features!
The logout
function is your best bet for logging a user out.
Signing users up can be a very specific thing for your site, so be sure to make that process fit your site. django-registration is a very popular package for customizing the registration workflow and one I definitely recommend checking out.
If you'd like to do authentication and registration through third-party sites, let letting a user sign up with their Twitter account, check out django-allauth or python-social-auth.
-
0:00
Users can log into the site, but they can't log out.
-
0:02
This is something they'll probably want at some time.
-
0:05
Thankfully, with all of Django's provided batteries,
-
0:07
this is as simple as a single function call.
-
0:10
Before a user can ever log in or log out though, they need to be able to sign up.
-
0:14
I think, we can tackle both of those things in this video,
-
0:17
logging out and signing up.
-
0:18
Join me in Workspaces to get started.
-
0:22
I actually already have a URL and a view for logging out.
-
0:26
Django provides me one in the URLs from the contrib package inside
-
0:32
these URLs right here, but let me show you what happens if I go there.
-
0:37
So, accounts/log out, kind of default standard name.
-
0:43
And I get this page, right?
-
0:45
It renders as admin template and not the template I like, right?
-
0:50
I would rather that the log-out view just redirect me back to the home page.
-
0:55
So, I'm gonna build that.
-
0:56
That's the nice thing, I can kinda just do the stuff I wanna do, right?
-
0:58
So, I'm gonna make a new url and
-
1:03
it'll be r"logout and
-
1:07
we go to views.LogoutView.as_view.
-
1:13
And the name will be logout.
-
1:15
All right, and then if I go over to views.py, I need to make the logout view.
-
1:21
Now that might seem backwards to some of you, making URL and making the view.
-
1:25
I guess, it kind of is, Django cares a little bit, I kind of complain, but
-
1:29
it doesn't really care.
-
1:30
You're just kind of do whatever you need to do.
-
1:32
So, I'm gonna import the logout function, which comes from the same place as login.
-
1:38
And then this is actually going to be a, Redirect view.
-
1:45
So, it's gonna be a view that just automatically redirects the user
-
1:48
at the end of it.
-
1:49
So, I'm gonna call this LogoutView and it's gonna be a RedirectView.
-
1:56
And redirect views require an argument named url,
-
1:59
which is where they redirect to.
-
2:01
So, I'm gonna redirect back to home.
-
2:03
And then in the get, which is what is fired when the get request comes in.
-
2:10
I'm gonna actually log the user out.
-
2:14
So, I'm gonna take the request, I'm gonna take the args and the kwargs.
-
2:17
I'm gonna call logout with the request, which since normally we
-
2:22
would login with the request, now we logout with the request.
-
2:27
So, same difference, I just called one request and
-
2:31
the other one got from the class.
-
2:34
And then we return super().get(request, *args, **kwargs).
-
2:39
Because I wanted to go ahead and do its normal return, right?
-
2:44
So, this view doesn't use a template or anything like that.
-
2:47
So, it's it's actually done.
-
2:50
So, If I don't typo.
-
3:00
So, I've been logged out.
-
3:01
So, that's awesome, and I can see the menu shows that I'm logged out and all of that.
-
3:07
Okay, so now what about signing up, right?
-
3:11
I need to be able to, I can log in, I can log out, that's great.
-
3:14
I can get in, I can get out, but I don't have any way of creating an account, so
-
3:18
I need to do that.
-
3:19
So, let's see, accounts/signup.
-
3:26
Now, and if I look here, there is no accounts like register or
-
3:31
create account or anything like that.
-
3:35
So, it must be that I need to make this view all by myself and it turns out, I do.
-
3:40
And that makes sense because creating an account is often very customized
-
3:44
to the site that it's on.
-
3:46
Okay, so I think, I can get by using a Django provided form for
-
3:50
creating a new user.
-
3:51
And I'll just write a create view for it.
-
3:55
It's worth a try, at least.
-
3:57
So, here in this, I'm only importing this AuthenticationForm, I'm also gonna import
-
4:02
UserCreationForm, and that's a form as you can probably guess for creating users.
-
4:08
So, then down here at the bottom, one two, I'm gonna make a SignUp view.
-
4:14
And I'm just gonna call this one SignUp.
-
4:17
So, this is going to be a CreateView because it's
-
4:22
creating a model and form_class = UserCreationForm.
-
4:28
The success_url should be
-
4:32
reverse_lazy("login").
-
4:38
That way they can go to the login page and
-
4:42
then template name is accounts/signup.html.
-
4:48
Okay, fairly straightforward and I'm gonna make that template.
-
4:52
So, signup.html, over here in accounts.
-
4:56
And I'm just gonna use this one, so I just paste it in the other one here.
-
5:00
So, it's gonna say, Sign Up and Sign Up, and
-
5:05
the form stays the same and then this would be Sign Up as well.
-
5:13
All right, I think that's everything.
-
5:15
So, different headline,
-
5:16
I bet you could think of a really good way to make it easier to change that headline.
-
5:21
Okay, so time to try this, all right?
-
5:24
Let's see, did I have a url for that?
-
5:26
I didn't, I need to add a URL.
-
5:27
All right, so, let's see, signup,
-
5:37
SignUp, signup, all right.
-
5:44
All right, so, here we go.
-
5:45
I've got my Username, I've got my Password and Password confirmation.
-
5:50
So, okay, let's try making a new one.
-
5:53
Let's say, testuser, And hit Sign Up.
-
6:00
That username already exists.
-
6:01
Okay, let's call it treehouse.
-
6:07
Okay, now I should be able to login with treehouse.
-
6:12
All right, not bad, I can log in, I can do all of this stuff.
-
6:18
But let me go back to this Sign Up form again, real quick.
-
6:24
So, one of the things that bugs me with this, is there's no email on here.
-
6:29
It says username, I know that is the username,
-
6:31
it's gonna show up as the username.
-
6:33
For right now though, I want this to say display name, right?
-
6:36
I don't know exactly what I want but for now that's what I want.
-
6:39
So, let's see, if I can change this form.
-
6:43
So, up here in accounts, I'm gonna make a new file, which is gonna be named
-
6:48
forms.py and then inside here I'm gonna import that same form, right?
-
6:53
So, I had this UserCreationForm here, so I'm gonna import that.
-
7:01
And while I'm at it, I'm gonna take it and then I'll leave it in the view for now.
-
7:04
Okay, so I've got that and then I need to import the model.
-
7:10
So, from django.contrib.auth.models import User, cuz that's the model, right?
-
7:17
So, I'm gonna make my own form here.
-
7:19
So, I'm gonna call this UserCreateForm instead of UserCreationForm.
-
7:24
But it's gonna extend the UserCreationForm.
-
7:26
And then I'm just gonna specify the class Meta,
-
7:29
and I'm gonna say the fields that I want it to add.
-
7:33
So, I wanna show the username field, I wanna show the email field and
-
7:38
then there's two fields named password1 and password2.
-
7:43
We should show both of those, so that's for
-
7:45
the password and the password validation.
-
7:48
And then the model will be User.
-
7:51
Okay, now I want to rename a couple of these fields,
-
7:56
I wanna change the label, at least.
-
7:58
So, I could redefine the fields in the form, but that's a lot of trouble,
-
8:02
cuz if you look, there's all this help text and stuff.
-
8:05
I don't wanna have to recreate all of that.
-
8:09
So, I'm just gonna override them here, inside the form.
-
8:12
So, self, *args, **kwargs.
-
8:17
And I'm gonna call super().__init, and that's gonna take *args and **kwargs.
-
8:24
And then I'm gonna say self.fields["username"].label
-
8:31
= "Display name".
-
8:34
And self.fields["email"].label = "Email address".
-
8:44
It may say something like that already but just to make sure.
-
8:47
Okay, so I think, it's gonna work, but
-
8:49
now I need to use this form in my view instead of the old one.
-
8:53
So, I'm gonna take that out and then we'll say, from .import forms.
-
8:59
And then down here at the bottom, forms.UserCreateForm.
-
9:05
Okay, so that should use my new form.
-
9:09
So, let me refresh this and yeah, look,
-
9:12
there's display name, email address, password and password, so, awesome.
-
9:17
There's the form that I want.
-
9:20
One thing, I didn't do that's a good idea,
-
9:22
is some sort of user validation on sign up.
-
9:24
I'm sure you've used a site where you had to click a link that was emailed to you to
-
9:27
verify, yep, I'm actually me, and I actually wanted to sign up.
-
9:31
That's a bit beyond the scope of this course, but I'll link to a library or
-
9:34
two in the teacher's notes that make it a lot simpler to implement.
-
9:37
Check those out before you put user registration on a live website.
-
9:41
I think, there's only one thing missing from the site as far as registration and
-
9:44
stuff is concerned.
-
9:45
What would your users do, if they forgot their password?
You need to sign up for Treehouse in order to download course files.
Sign up