Django OTP

1.7.0 · active · verified Fri Apr 10

django-otp is a pluggable framework designed to integrate two-factor authentication (2FA) into Django projects using one-time passwords (OTPs). It provides the core framework and common OTP algorithms (HOTP, TOTP), allowing developers to build custom 2FA solutions or leverage existing plugins. The current version is 1.7.0, and the project maintains a healthy release cadence with active maintenance.

Warnings

Install

Imports

Quickstart

This quickstart outlines the essential steps to integrate `django-otp` into a Django project. It involves adding `django_otp` and at least one OTP plugin (e.g., `otp_totp`) to `INSTALLED_APPS` and enabling `OTPMiddleware`. For admin site integration, assign `OTPAdminSite` to `admin.site.__class__`. Remember to run `python manage.py migrate` after updating `INSTALLED_APPS` to create necessary database tables.

# settings.py

INSTALLED_APPS = [
    # ... other Django apps
    'django_otp',
    'django_otp.plugins.otp_totp',  # Example: Time-based One-Time Passwords
    # 'django_otp.plugins.otp_hotp', # Example: HMAC-based One-Time Passwords
    # ... other OTP plugins (e.g., django-otp-sms)
]

MIDDLEWARE = [
    # ... other Django middleware
    'django_otp.middleware.OTPMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware', # Ensure this is before OTPMiddleware
    # ...
]

# urls.py (example for admin integration)
from django.contrib import admin
from django.urls import path
from django_otp.admin import OTPAdminSite

admin.site.__class__ = OTPAdminSite

urlpatterns = [
    path('admin/', admin.site.urls),
]

# After configuring, run migrations:
# python manage.py migrate

view raw JSON →