Last Updated : 11 Jul, 2025
Django-allauth is a powerful Django package that simplifies user authentication, registration, account management, and integration with social platforms like Google, Facebook, etc. It builds on Django’s built-in authentication system, providing a full suite of ready-to-use views and forms.
Features of Django allauth:Prerequisite: Django-allauth setup and Configuration
In this tutorial, we will set up a basic Django application that demonstrates how to use django-allauth for user authentication- covering login, logout, and signup functionality without using any custom templates.
Step 1: Create a Django Project and AppCreate a new project and app, to learn about how to create and set it up, refer to:
Suppose we created a project named form and an app inside it, named formapp.
Register formapp in settings.py so Django knows to include it in the project:
Python
INSTALLED_APPS = [
...
'formapp', # formapp registered here
]
Step 2: Install and Configure django-allauth
We install django-allauth to extend Django's authentication system with features like registration, login, logout, email verification, and social login. It provides prebuilt views and forms so we don't have to implement them manually.
Install it using the command:
Add required apps to INSTALLED_APPSpip install django-allauth
We now add required apps that power the core features of django-allauth, including site management and social accounts.
Python
INSTALLED_APPS = [
...
'django.contrib.sites', # Required by allauth to manage sites
'allauth', # Core allauth functionality
'allauth.account', # User account management (signup/login)
'allauth.socialaccount', # For social login (optional)
'formapp', # Your app
]
Configure SITE_ID
This is needed for Django’s sites framework. Allauth uses this to differentiate between multiple domains. Since we’re using one site, we set:
Set up authentication backendsSITE_ID = 1
We need to tell Django to use both its default backend and the one provided by allauth, add these codes in settings.py:
Python
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend", # Default auth
"allauth.account.auth_backends.AuthenticationBackend", # Required by allauth
)
Define redirect URLs after login/logout
These control where users are redirected after they log in or log out:
Python
LOGIN_REDIRECT_URL = '/' # Redirect after successful login
ACCOUNT_LOGOUT_REDIRECT_URL = '/accounts/login/' # Redirect after logout
Customize account behavior
Here we define what fields are required and how authentication should behave:
Python
ACCOUNT_EMAIL_REQUIRED = True # Email must be provided
ACCOUNT_USERNAME_REQUIRED = True # Username is also required
ACCOUNT_AUTHENTICATION_METHOD = 'username_email' # Users can log in using username or email
ACCOUNT_EMAIL_VERIFICATION = 'none' # Skip email verification for simplicity
Ensure required middleware and context processors are present
allauth needs access to the request object in templates. Add the following to TEMPLATES:
'django.template.context_processors.request',
And include the AccountMiddleware:
Step 3: Create a Custom Signup Form'allauth.account.middleware.AccountMiddleware',
Sometimes you want to collect additional user data or change signup behavior. We do this by creating a form that extends SignupForm.
Create the following inside formapp/forms.py (make sure it is inside the app folder, not the project folder):
Python
from allauth.account.forms import SignupForm
from django import forms
class CustomSignupForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First Name')
last_name = forms.CharField(max_length=30, label='Last Name')
def save(self, request):
user = super().save(request)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user
Then, tell Django to use this form:
Python
# settings.py
ACCOUNT_FORMS = {
'signup': 'formapp.forms.CustomSignupForm',
}
This replaces the default signup form with your custom one.
Step 4: Set Up URLsdjango-allauth provides ready-made views like /login/, /signup/, /logout/. To enable them, include its URLs in the project’s urls.py:
Python
# form/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
]
Now visiting /accounts/login/ or /accounts/signup/ will render the custom signup form (if defined), or the default form otherwise.
Step 5: Run the Server and TestMigrate the database to create necessary tables:
python manage.py makemigrations
python manage.py migrate
Open your browser and visit:
Outputs:
Snapshot of the sign-up page Snapshot of the sign-in pageRead Next Article: Django Sign Up and login with confirmation Email
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4