Google Authentication in Django

In many developer websites, we get to see Google social authentication which is very handy. In this article, we will see how to create a Django Google login project.

  • Then, select web application, and add these two URLs −

Now, you will get a client ID and a secret key, keep them safe in your file.


Create a Django project and an app.



   "django.contrib.sites", # <--
   "allauth", # <--
   "allauth.account", # <--
   "allauth.socialaccount", # <--
   "googleauthentication" #this is my app name ,you can name your app anything you want
   'google': {
      'SCOPE': [
      'AUTH_PARAMS': {
         'access_type': 'online',

#add this in the end of file

Here, we defined a Redirect URL. In INSTALLED APPs, we defined important backends that we are going to use for authentication. Then, we defined the social account provider which will tell what we should use for login (here we are using Google). We also defined what data of user should it store.

Now, in project's, add the following −

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

urlpatterns = [
   path("accounts/", include("allauth.urls")), #most important
   path('',include("googleauthentication.urls")) #my app urls

Here, we added the default path that is needed to add; it is allauth library path for enabling Google login. The second one is app path that we made.

In app's

from django.urls import path
from . import views
urlpatterns = [

Here, we set up our urls and rendered our views on home url.


from django.shortcuts import render

# Create your views here.
def home(request):
   return render(request,'home.html')

We simply rendered the frontend here.

Create a template folder in app's main directory and add a file home.html with the following content −

<!DOCTYPE html>
      <title>Google Registration</title>
      {% load socialaccount %}
      <h1>My Google Login Project</h1>
      <a href="{% provider_login_url 'google'%}?next=/">Login with Google</a>

Here, we rendered the JS and loaded the allauth library on frontend. In <a> we provided the Google login page on which we set up our default Google login page.

Now, run the following commands on the terminal −

python makemigrations
python migrate

Next, create a superuser.

python createsuperuser

Then, start the server and go to admin panel. Go to Sites and add a site with url name and display name:

Go to Social applications and add application. Choose the site you added before −

This will register Google on your Django project as an authentication backend. Everything is set, now you can proceed to check the output.