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