Django social auth demo

Today, 25th June 2022, Ukraine is still bravely fighting for democratic values, human rights and peace in whole world. Russians ruthlessly kill all civilians in Ukraine including childs and destroy their cities. We are uniting against Putinโ€™s invasion and violence, in support of the people in Ukraine. You can help by donating to Ukrainian's army.

This example explains how to authorize via Facebook in Django using OAuth2. Note that Django 1.10 used, some options in may differ in older versions.

Register facebook application and get keys

Go to, press:

Image for a hint

Add Facebook Login product, go to Settings and add http://djsoc.local:8000/complete/facebook

Image for a hint

Note that localhost:8000 or can't be used, but djsoc.local:8000 is ok

Go to Dashboard and see your App ID and App Secret, you will need to use them in

Image for a hint

Configure Django

Install social-auth-app-django

pip install social-auth-app-django

Add to the end of

ALLOWED_HOSTS += ['djsoc.local']



  'locale': 'ru_RU',
  'fields': 'id, name, email, age_range'


TEMPLATES[0]['OPTIONS']['context_processors'] += [

Modify main

from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic.base import TemplateView
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='main.html')),
    url(r'^logout/', auth_views.logout, name='logout'),
    url('', include('social_django.urls', namespace='social')),

Add template main.html:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <title>Django Social Auth demo</title>
You logged in as: {{ request.user }}
<a href="{% url "social:begin" "facebook" %}?next={{ request.path }}">Auth via Facebook</a>
<a href="{% url "logout" %}?next=/" >Logout</a>


Add djsoc.local to hosts:

echo " djsoc.local" >> /etc/hosts

Migrate and run server

python migrate runserver djsoc.local:8000

Ready for production?

After you test all staff go to App Review tab and make app public

Image for a hint

#django #oauth2
Ivan Borshchov profile picture
Jan 29, 2017
by Ivan Borshchov
Did it help you?
Yes !

Best related