How to use django-allauth

How to use django-allauth

django-allauth documentation seems insufficient for the first time user. But trust me it’s not!! There are lots of settings and configuration in django-allauth that it is quite difficult to document in perfect way for new users as new users don’t have much idea about how this django-allauth actually works. So this guide will help new users how to use django-allauth for Google sign in.

Here’s is the step by step guide on how to use django-allauth

  1. First thing is to download all required files from GitHub

    Click to open GitHub page for django-allauth

    Then click on DOWNLOAD ZIP and download the zip file as shown below.

  2. Then extract all the files using extract here option and open the extracted folder named django-allauth-master. Now copy the requirements.txt to allauth folder. As shown in figure.

  3. Now copy the allauth folder into open your django project base_directory where manage.py exists (in my case, test1 is the project name) . Look into the figure below.

  4. Now open CMD and CD into allauth folder which is pasted in previous step (perform this step with virtualenv activated!!). and run the following command.
    pip install -r requirements.txt

  5. Now it’s time to edit settings.py file in the project directory. (In my case, project name is test1 so, opening settings.py in test1 folder and test1 folder is present where manage.py file exists)

    Edit INSTALLED_APPS as below:

    INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #Following is required!
    'django.contrib.sites',
    #allAuth apps
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    )

    Edit context_processors as below for Django<=1.7. And please do not remove already existed values (if you have) in following blocks!!!

    TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    )

    Edit context_processors as below for Django>=1.8. And please do not remove already existed values (if you have) in following blocks!!!

    TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
    'context_processors': [
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    ],
    },
    },
    ]

    Edit AUTHENTICATION_BACKENDS as below. And please do not remove already existed values (if you have) in following blocks!!!

    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',
    )

  6. Now run the following command in virtualenv of this project:

    python manage.py migrate # for Django>=1.7
    python manage.py syncdb # for Django<=1.6

    I’m considering you already have setup admin account (superuser in django). If no please set up superuser and then continue to login with admin credentials

    localhost:8000/admin

  7. In admin dashboard page, go to sites and add new site:

  8. Fill Domain name as localhost:8000
    and Display name: localhost
    and then save. It will save Site in database table named django_site

  9. Now add new value in Social Application by click add as shown in below diagram:

  10. Now add values as shown in the below screen shot. Please get your Clien ID and secret key from google developer API console. If you don’t have it already click here and follow the steps

  11. Add SITE_ID in settings.py file by adding below line

    SITE_ID = 1

    So how to find the SITE_ID. Open any tool that can access database. In my case I have MySQL so I’m using MySQL command line.
    Then used following commands:

    use final_db; #final_db is database name in settings.py
    select * from django_site;

    You can use SITE_ID which you want to use. If you are inserting for first time your SITE_ID will be 1

  12. Now finally, you need to enable URL pattern for this. Open urls.py file in project directory. In my case base_directory/test1/urls.py

  13. Edit the urls.py file and add following line into it:

    urlpatterns = patterns('',
    #keep all other url patterns intact
    url(r'^accounts/', include('allauth.urls')), #Required for allAuth
    )

  14. That’s it you are done! Now you can open the URL

    localhost:8000/accounts/login/

    You will see the page similar to below. In my case it’s without CSS 😉
    Now you can click on Google option:

  15. Now complete the google sign in procedure. You will be logged in using google account.

  16. New user will be added to our database with email and all other required information. You can check from database as below:

    select * from auth_user;
    select * from account_emailaddress;
    select * from account_emailconfirmation;

  17. that’s it you are done! In case of any doubts in how to use django-allauth, you can comment below. 🙂

4 comments

  1. ayushi Reply

    Hi,
    I’m new to django and trying to set up django AllAuth google.
    When i hit localhost:8000/accounts/login/ i get redirected to localhost:8000/accounts/profile automatically and get the below error:

    Page not found (404)
    Request Method: GET
    Request URL: http://localhost:8000/accounts/profile/
    ………….
    The current URL, accounts/profile/, didn’t match any of these.

    • Ankit Wasankar Reply

      You have been redirected to profile page that means you have successfully logged in. However, you will need to implement a view for this URL – /accounts/profile yourself, as whatever is to be displayed here is project specific. You can also decide to redirect elsewhere.
      You need to write the view for accounts/profile by yourself. Or you can also change the default redirect URL from LOGIN_REDIRECT_URL = “/profile” whatever you wish and register the new view with this URL for your profile.

Leave a Reply

Your email address will not be published. Required fields are marked *