Setting Up django-allauth6:56 with Kenneth Love
Now that we have our GitHub tokens, we can install and configure django-allauth.
Before, I can use Django-allauth, I need to install it. 0:00 I already have a project started, 0:03 it's almost the same as the one that was used in the Django authentication course. 0:05 I've removed the custom user model, the manager, the off user there, 0:08 we're not gonna use them and the forms like that. 0:12 I won't need those things for this course. 0:15 You can use Django-allauth with custom users though, if you want. 0:17 I'm just not going to for 0:20 this one because it adds some complication that I don't think we need at this point. 0:21 I'm also going to do all of this locally instead of in work spaces. 0:24 Because I need to GitHub to be able to easily redirect back to the running site. 0:27 Now it's totally possible in workspaces, 0:31 it's just easier if I do everything locally. 0:33 So I've activated my virtual env. 0:36 So now I'm going to pip install django-allauth. 0:38 And at this point, I don't feel like I need to specify an exact version, but 0:42 if you want to go ahead and specify the version we're using 0.27.0. 0:46 I can't make promises that all of these instructions will work on future versions, 0:51 but they should. 0:54 Now most of the rest of the work that I need to do, at least for 0:56 this video is in settings and URLs, and a little tiny bit inside the admin. 0:59 So the first thing I'm gonna do. 1:04 I'm going to hop over here to Adam, into the settings 1:05 file, and I need to add some settings. 1:12 The first one that I need to add is AUTHENTICATION_BACKENDS, 1:18 and this is a tuple and you can specify by multiple backends in this. 1:25 This setting controls which backends are used for authenticating users. 1:29 And the first item is used first, and then if no accounts are found through 1:33 that authentication backend, the second is used and so on. 1:37 So I'm actually on the list django.contrib.auth.backends.ModelBackend. 1:41 And I'm gonna add 1:48 allauth.account.auth_backends.Authenticat- ionBackend. 1:50 And we have to have the default Django backend in there so 1:57 that we can still log in with normal super user accounts in the admin. 2:00 And it actually doesn't matter which order you put these two items in, 2:04 especially when it's just these two. 2:06 The superuser account wouldn't be found inside the allauth package. 2:09 And the allauth users, 2:12 the social users, wouldn't be found inside the Django model backend. 2:14 So regardless of the order you have these, 2:17 the lookups will just fall through until they find the right one. 2:19 So the next thing I need to do is I need to add a couple of things to 2:23 INSTALLED_APPS. 2:26 So, I'll scroll up here and find INSTALLED_APPS. 2:27 So, I need to make sure that the django.contrib.sites library is in here. 2:33 So I'm gonna add sites right there. 2:40 And then you to add some stuff for allauth. 2:44 So the first thing you need to add is the actual allauth app itself. 2:48 The second thing I need to add is allauth.account and allauth.socialaccount. 2:52 And these two control like creating accounts and creating social accounts and 3:00 all of that stuff. 3:03 So these three apps together, not counting the Django.contrib.sites one, 3:04 will give us all the models, the templates, the views. 3:08 All that stuff we need for this entire process. 3:11 I also need to add a fourth app here, 3:13 which is allauth.socialaccount.providers.github, 3:16 because I want to use GitHub as a provider. 3:24 So this is the one that will actually handle creating accounts based on 3:26 GitHub tokens. 3:29 One last setting that we need for all off to work, and 3:30 it relates to this site's thing, is we need to add the site ID. 3:34 So I'm gonna add this at the bottom again. 3:38 SITE_ID = 1. 3:41 Django can run multiple sites with multiple domains 3:43 from one Django installation. 3:47 This comes in handy if you have the same data but need multiple domain names, for 3:48 example, if you were running a regional newspaper but you had mastheads for 3:52 each school district. 3:56 You could have multiple domain names one for each masthead. 3:57 And show stories from the same pool of articles on each of those sites. 4:00 This is our first and only site. 4:04 So we're gonna set the ID to 1. 4:07 So now for our URLs. 4:08 So go to MSG go to url.pie and I'm gonna add a new set of URLs. 4:11 I've got all of these and I'm going to add one new and 4:18 down at the bottom which is also going to be accounts. 4:22 And I'm going to include 4:27 all off that URLs I'm not gonna give us a namespace or anything like that. 4:30 And we should now run the migrations that come with allauth. 4:34 So python manage.py migrate and this applies our account migrations, 4:38 our sites migrations, and our social account migrations. 4:45 And the last thing that we need to do is set up some things in admin. 4:48 So I'm gonna run the server and 4:53 then I'm actually gonna come over here to an incognito window, 4:56 because I want to be able to have an admin account that's just always logged in. 4:59 So I'll log in with the usual data, 5:03 and now I need to add a couple of things in here. 5:06 The first thing I need to do is I need to set up the site. 5:09 So if I click on sites, and then I click on this example.com, 5:11 which is the default one that comes in, I need to set the domain name. 5:14 Now we don't include http or anything like that. 5:18 So we just put in that, and we hit save. 5:21 And you can rename this from example.com to something else if you want, 5:25 it won't matter for what we're doing and 5:29 that's because everything will just use the actual domain name. 5:31 This way if there are any URLs that are constructed by putting the domain name on 5:34 something, it will use the right one. 5:38 The last thing I need to do is set up the social app for the get up credentials. 5:41 Down to the bottom, you'll see this one called social applications, go ahead and 5:44 click Add. 5:49 The provider is going to be GietHub because that's our only provider. 5:50 The name is whatever we want it to be, so I'm going to say MSG Treehouse just so 5:53 that it matches, and then the client ID and the secret key come from GitHub. 5:58 So back over here at your GitHub account that you created. 6:02 There is a Client ID Which you'll want to copy and paste into here. 6:06 And then there is a Client Secret, which you will want to copy and paste into here. 6:12 Now if you're doing what I'm doing, and you're putting all this information 6:19 live on the Internet for people to see, you'll want to revoke all of these tokens 6:23 on these secrets, after people see that, because you don't want it to be revealed. 6:28 For what I'm doing now, it'll be okay. 6:33 It's not gonna be a live thing. 6:35 The last thing that I need to do over here is I need to make sure the keys and 6:37 everything are associated with this site, the site that I created a moment ago. 6:40 So, now we're good. 6:45 I'm gonna hit Save, and everything is all set up. 6:46 So, in our next video, I'll be ready to use allauths features into the templates. 6:49 And use the entire site. 6:54
You need to sign up for Treehouse in order to download course files.Sign up