Anda di halaman 1dari 3

Installation

On C:\Python35-32>python3 m pip install django-allauth to install the latest


version.

Settings

Your next step is to make a few changes to your settings.py file. Start by registering
allauth in INSTALLED_APPS:

INSTALLED_APPS = (
'allauth',
'allauth.account'
'allauth.socialaccount',
)

Next, you need to manually set your SITE_ID variable in settings.py. Why? Because allauth
uses this to fill in email and html templates with information about your sites name and
domain. First, add SITE_ID = 1 in settings.py.

Then run the localhost server and log in to your projects admin at localhost:8000/admin. If
you dont yet have a superuser account yet you can create one by navigating to the root of
your project and using the python manage.py createsuperuser command. Just follow the
prompts to create a superuser.

After logging in to your admin you should see a Site section in the dashboard. Now you can
change the sites name and domain (unless your domain happens, through some freak
accident, to be example.com).

Go back to settings.py, and add your authentication backends, like so:

AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of
`allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by
e-mail
"allauth.account.auth_backends.AuthenticationBackend"
)

Last but not least, lets tell allauth which url to route to when login is successful.
Add LOGIN_REDIRECT_URL = '/awesome-page/' to your settings.py, and change awesome-
page to whatever page you want to redirect to after users successfully log in.

Routing

Now lets focus on configuring our routers. Go to urls.py on ecomstore and add the
following to your urlpatterns to register the allauth urls:

urlpatterns = [

#You don't have to use "accounts," but I recommend it to


keep
#authentication separate from the rest of your app.
url(r'^accounts/', include('allauth.urls')),

Now would be a good time to familiarize yourself with all the free urls that allauth comes
with. These urls are a map of the fairy dreamland of easy password resets and email
confirmations that allauth provides.

As a quick sanity check, lets fire up our server and see if everything is working smoothly.
Navigate back to your root directory, run python manage.py runserver and go to
localhost:8000/accounts/login. You should see allauths hideous stock login form. Dont
worry - its easy to prettify these templates.

Custom Templates

Setting up allauth with custom templates is easy. Simply add an accounts directory to the
templates directory in your app. To override an allauth template, just create a
new .html file with the same name. In this case, you can create one for login.html.

You can use the login template in allauth.templates.account as an example. Just remember:
if youre copying allauths templates, you need to write a base.html to inherit from!
Success

You have your nice custom login page set up, right? If so, congrats! Try logging in with the
superuser you already created, and see if it redirects to the LOGIN_REDIRECT_URL you
specified in settings.py. Thats all there is to it!

Actually thats not all there is to it - check out the list of configuration options in the allauth
docs to see all the ways you can bend allauth to your will. Youll probably need to use at
least one or two of these to fit your use case.

If youve followed along this far, you should be good to go. Now sit back and watch as
millions upon millions of users register with your beautiful, easy-to-use authentication
system.

Anda mungkin juga menyukai