Geek Culture
Published in

Geek Culture

Django Social Authentication: Sign In With GitHub

How to sign in and sign up using GitHub in Django framework web apps

Login with GitHub using the Django framework

Hello, Django Developers..!!

My name is Rohit Kumar Thakur. I generally write about React Native, Django, Data Science, Machine Learning, and Python.

In this article, I am going to show you how to use social authentication in Django applications. When you are building a web application, then adding GithHub authentication is the most common feature you can add. It will help users to log in to your website easily. So, without wasting any time let’s start this project.

Here is the step by step video tutorial of this article:

Project Setup

  • I always suggest starting a Django project in a virtual environment because it’s a good habit to practice.
  • Choose a directory. Open the terminal or command prompt window in that directory and start a virtual environment. Activate that virtual environment. I wrote a separate article on how to start a virtual environment in Windows and Linux. You can check if you need help.
  • After activating the virtual environment, install the following python library:
pip install django
pip install social-auth-app-django
  • Start a Django project:
django-admin startproject github
cd github
python startapp mainApp
  • Open this project in your favorite text editor. I mean VS Code. xd:)


'social_django', #add this
'mainApp', #add this
'social_django.middleware.SocialAuthExceptionMiddleware', #add this
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'context_processors': [
'social_django.context_processors.backends', #add this
#Add this at the bottom in
#social app custom settings

LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
SOCIAL_AUTH_GITHUB_KEY = '############'
SOCIAL_AUTH_GITHUB_SECRET = '########################'

In the We need to add a few lines of code to INSTALLED_APPS, MIDDLEWARE, and TEMPLATES sections. After that, we have to add a few lines of code at the bottom as shown above.

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
# Create your views here.def login(request):
return render(request, 'login.html')
def home(request):
return render(request, 'home.html')

In, first, we are going to import all the required libraries. After that, write two functions to render the login and home screen.

Now inside the mainApp directory. Make a new directory “templates”. Inside the templates directory, make three new files: base.html, login.html, home.html

Make a static folder too. Inside the mainApp directory. And inside the static directory, make a file “index.css

You can customize the CSS part on your own if you want. But for a minimal UI reference, you can add the following codes to the index.css file.

.container-fluid {
height: 100vh;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
}.container-fluid > div {
width: 85%;
min-width: 300px;
max-width: 500px;
width: 100%;
background-color: #282c34;
}.social-container {
display: flex;
flex-direction: column;
justify-content: center;
}.btn a, .btn a:hover {
color: white;
text-decoration: none;


{% load static %}<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link href="" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'index.css' %}" />
<title>DJANGO SOCIAL AUTH</title>
<div class="container-fluid">
<h1 class="text-white text-center">
{% block title %}
{% endblock %}
<div class="card p-5">
{% block content %}
{% endblock %}

This is the code of the base.html file. We are going to take reference and relative tag elements in login.html and base.html.


{% extends 'base.html' %} 
{% block title %}Sign in{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-8 mx-auto social-container my-2 order-md-1">
<button class="btn btn-primary mb-2">
<a href="{% url 'social:begin' 'github' %}">
{% endblock %}


{% extends 'base.html' %} 
{% block title %}HOME{% endblock %}
{% block content %}
<div class="row">
<div class="col-sm-12 mb-3">
<h4 class="text-center">Welcome {{ user.username }}</h4>
{% endblock %}

This is the basic code of login and home screen. After the successful login, we are going to display the name of the user on the home screen.


from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from mainApp import views
urlpatterns = [
path('login/', views.login, name='login'),
path('logout', auth_views.LogoutView.as_view(), name='logout'),
path('social-auth/', include('social_django.urls', namespace='social')),
path("", views.home, name='home'),

Add the URLs in the main directory.

Configure Github Developer Console

Now we have to do only one thing. Let’s set up Github Developer Profile. I recommend you watch the video after 18:32. The video is here.

  • You need a GitHub account.
  • Click on your profile. Then click on settings
  • Scroll down and click on developer settings (Left-Hand side)
  • Click on OAuth Apps
  • Click on Register a new application
  • Add application name: test
  • Homepage URL: http://localhost:8000
  • Authorization callback URL: http://localhost:8000/social-auth/complete/github/
  • Click on Register application
  • Generate client secret code.
  • Copy the client id and client secret and past it here:
SOCIAL_AUTH_GITHUB_KEY = '############'
SOCIAL_AUTH_GITHUB_SECRET = '########################'
  • If you checked the video then make sure to hit the subscribe button. I make cool Django and react native applications. Your support will be admired.

Now, Open your terminal or command prompt and migrate the Django application.

python makemigrations
python migrate
python runserver

Now click on the “Login with GitHub” button. You are good to go now.

GitHub Code Link

Now, clap, clap and clap! And do follow for more project-based articles on Django Framework.

Thanks for reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store