Google Authentication in Django

Hello Internet Programmers. Let’s add google authentication in Django projects. We will use django-allauth packege. Okay, let’s jump into the code.

We are continuing from the previous tutorial: Django-allauth Customizing form templates and adding CSS

But don’t worry you can still follow this for your project directly.


pip install django-allauth

Setup django-allauth

Open and do following changes (Important – Please note ‘django.contrib.sites’ is required as INSTALLED_APPS)

Add following lines in INSTALLED_APPS


    'django.contrib.sites', # must
    'allauth', # must
    'allauth.account', # must
    'allauth.socialaccount', # must
    '', # new

You can see we include the google providers for authentication.

Then at the bottom of we need to specify that we’re using the allauth backend, add a SITE_ID since allauth uses this, and configure a redirect to the homepage upon successful login.

# core/


    # Needed to login by username in Django admin, regardless of `allauth`

    # `allauth` specific authentication methods, such as login by e-mail



we already enabled email verification if you want to enable it see: django-allauth email authentication or just continue this

Now open of the main project and add this,

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('accounts/', include('allauth.urls')),

Setup Google APIs

First, go to and create a project.

Google cloud's new project for Django social auth with google

Google cloud’s new project

Now go to the Oauth consent screen and fill in the information and save & continue.

Oauth consent screen django google authentication

Oauth consent screen

Go to credentials and click create credentials and then click OAuth client ID

OAuth client ID django google registration

OAuth client ID

Click on it and choose web application, and add these two URLs,

Create OAuth client ID

Create OAuth client ID

Click on Create and You get your Client ID and Client Secret. Copy those keys.

OAuth client id and client secret, django-allauth google authentication

OAuth client id and client secret

Add a social app in Django admin

First, migrate

python migrate

assuming you’ve already created a superuser if not then create using,

python createsuperuser

Now run the server using python runserver and open admin. Go to this page and make these changes,

Change site in Django admin, google authentication django-allauth

Change site in Django admin

Then, click on Social Applications click Add and fill in the details as follows

  1. Provider: Google
  2. Name: OAuth App
  5. Sites:

Add Social application Django admin, google auth django-allauth

Add Social application Django admin

Now logout yourself because you logged in as a superuser and open and you can see Google login option.

google auth option on login page, django-allauth google authentication

Google auth option

Now test and signup with google. After signing in with Google, you can check the user information obtained from Google at

If you have customized django-allauth templates then you can directly use this code in your template.

{% load socialaccount %}
<h1>Google Login</h1>
<a href="{% provider_login_url 'google'%}?next=/">Login with Google</a>

Okay, that’s it. Happy Coding