Site icon StudyGyaan

Add Facebook Login to Django Website using django-allauth

Django Web Framework Tutorials

In today’s interconnected world, user authentication is a vital component of web development. Integrating third-party authentication providers like Facebook can enhance user convenience and streamline the registration process for your Django website. In this tutorial, we’ll guide you through the process of adding Facebook login to your Django project with a practical example.


  1. Basic knowledge of Django.
  2. A Django project up and running.

Let’s get started!

Step 1: Create a Django Project

If you haven’t already, create a Django project using the following command:

django-admin startproject myproject

Recommeded: Please read our blog, on how we create Basic Django Project

Step 2: Set Up Your Django Application

Create a Django app within your project:

cd myproject
python startapp myapp

Add your app to the INSTALLED_APPS list in myproject/

    # ...
    # ...

Step 3: Configure Facebook Developer Account

To enable Facebook login, you need to create a Facebook Developer account and set up OAuth 2.0 credentials.

Step 4: Install Required Libraries

You’ll need to install some Python libraries to handle OAuth2 authentication and FB integration. Run the following commands:

pip install django-allauth

Step 5: Configure Django Allauth

Add 'allauth' and 'allauth.account' to the INSTALLED_APPS and MIDDLEWARE list in your file:

    # ...
    'myapp', # Your App
    # ...


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

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


    'facebook': {
        'METHOD': 'oauth2',
        'SCOPE': ['email', 'public_profile'],
        'APP': {
            'client_id': 'YOUR_APP_ID',
            'secret': 'YOUR_APP_SECRET',

Replace 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' with the values you obtained from the Facebook Developers Console.

Step 6: Create a Custom User Model (Optional)

The default Django User model includes fields like username, email, and password, which cover the basics of user authentication and management. However, real-world applications often demand more user-specific attributes and functionalities. Imagine a scenario where you’re building a social platform or an e-commerce site. You might need to store user profiles, social media links, user avatars, or even track user activity.

Learn more about How to Extend the Django User Model: Exploring Various Approaches

Step 9: Update URLs

In your myproject/ file, add the following URL patterns:

from django.urls import path, include

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

Step 9: Create Templates (Optional)

Create templates for login and account management in the myapp/templates/ directory, following Allauth’s templates structure.

Step 10: Migrate and Run the Server

Run the following commands to apply migrations and start the development server:

python makemigrations
python migrate
python runserver

Visit http://localhost:8000/accounts/login/ to see your facebook login in action.

You can also check the users account list in Django Admin. Create a superuser – python createsuper and go to http://localhost:8000/admin/

Congratulations! You’ve successfully added Facebook login to your Django website. Users can now sign in with their Facebook accounts, simplifying the registration process and enhancing their user experience.

Ensure responsible handling of user data and permissions when integrating third-party authentication to maintain the privacy and security of your users’ information while providing a seamless login experience.

Read Next: Learn how to protect your CLIENT ID AND SECRET in python project.

Find this tutorial on Github.

Also check our blog for Github and Gmail Login

Blogs You Might Like to Read!
Exit mobile version