django-allauth

65.15.1 · active · verified Fri Apr 10

django-allauth is an integrated set of Django applications that provides comprehensive solutions for authentication, registration, account management, and third-party (social) account authentication. It is actively maintained with frequent releases, often aligning with new Django and Python versions. It aims to offer a unified approach to both local and social authentication flows.

Warnings

Install

Imports

Quickstart

To quickly set up `django-allauth`, first add the necessary apps (`django.contrib.sites`, `allauth`, `allauth.account`, and optionally `allauth.socialaccount` and providers) to your `INSTALLED_APPS`. Configure `SITE_ID=1`. Update `AUTHENTICATION_BACKENDS` to include `allauth.account.auth_backends.AuthenticationBackend`. Ensure `django.template.context_processors.request` is in `TEMPLATES`. Add `allauth.account.middleware.AccountMiddleware` to `MIDDLEWARE`. Finally, include `allauth.urls` in your project's `urls.py` under the `/accounts/` path. Basic settings for email authentication and redirection are also common. Remember to run `python manage.py makemigrations` and `python manage.py migrate` after configuration.

import os

# settings.py
# Add to INSTALLED_APPS (order matters, 'django.contrib.sites' and 'allauth' apps come after Django's built-in apps)
INSTALLED_APPS = [
    # ... django defaults
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount', # Optional: if using social logins
    # ... add specific social providers here, e.g., 'allauth.socialaccount.providers.google',
]

SITE_ID = 1 # Must be set to 1 for allauth to function correctly

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend', # Required for Django admin
    'allauth.account.auth_backends.AuthenticationBackend', # allauth specific backend
]

# Required context processor for allauth templates
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# allauth specific settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_SIGNUP_EMAIL_ENTER_WITHOUT_REQUEST = True # Enable instant signup with email
ACCOUNT_AUTHENTICATION_METHOD = 'email' # Allow login with email, not username
ACCOUNT_EMAIL_VERIFICATION = 'mandatory' # Or 'optional', 'none'
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1 # How long email verification links are valid
LOGIN_REDIRECT_URL = '/'
ACCOUNT_LOGOUT_REDIRECT_URL = '/'

# Add allauth middleware
MIDDLEWARE = [
    # ... other middlewares
    'allauth.account.middleware.AccountMiddleware',
]

# For testing email in development
if os.environ.get('DJANGO_SETTINGS_MODULE') == 'your_project.settings': # Example check
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# urls.py (in your project's main urls.py)
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
    # ... other paths
]

view raw JSON →