Djoser (Django REST Authentication)

2.3.3 · active · verified Thu Apr 16

Djoser provides a set of Django Rest Framework views to handle basic actions such as registration, login, logout, password reset, and account activation. It handles user authentication for Django REST Framework APIs, supporting various authentication backends including Token and JWT. The current version is 2.3.3, and it maintains an active release cadence with regular updates to support new Django and Python versions.

Common errors

Warnings

Install

Imports

Quickstart

To integrate Djoser, first add `rest_framework` and `djoser` to your `INSTALLED_APPS`. Configure `AUTH_USER_MODEL` to point to your desired user model. Define `REST_FRAMEWORK` and `DJOSER` settings in your `settings.py` for authentication classes, permissions, and Djoser-specific URLs and serializers. For JWT, install `djangorestframework-simplejwt` and include `djoser.urls.jwt`. Finally, include Djoser's URLs in your project's `urls.py`.

import os
from datetime import timedelta

# settings.py

INSTALLED_APPS = [
    # ... other Django apps
    'rest_framework',
    'djoser',
    # 'rest_framework_simplejwt', # Required for JWT support
    # 'your_app_name', # If using a custom user model in 'your_app_name'
]

# Point to your custom User model or Django's default
AUTH_USER_MODEL = 'auth.User' # Or 'your_app_name.CustomUser'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        # 'rest_framework.authentication.TokenAuthentication', # If using Token Auth
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticatedOrReadOnly',
    ),
}

DJOSER = {
    'PASSWORD_RESET_CONFIRM_URL': '#/password/reset/confirm/{uid}/{token}',
    'USERNAME_RESET_CONFIRM_URL': '#/username/reset/confirm/{uid}/{token}',
    'ACTIVATION_URL': '#/activate/{uid}/{token}',
    'SEND_ACTIVATION_EMAIL': True,
    'SEND_CONFIRMATION_EMAIL': True,
    'SET_PASSWORD_RETYPE': True,
    'SET_USERNAME_RETYPE': True,
    'PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND': True,
    'TOKEN_MODEL': None, # Use Simple JWT by default
    'SERIALIZERS': {
        'user_create': 'djoser.serializers.UserCreateSerializer',
        'user': 'djoser.serializers.UserSerializer',
        'current_user': 'djoser.serializers.UserSerializer',
        'user_delete': 'djoser.serializers.UserDeleteSerializer',
    },
    'EMAIL': {
        'activation': 'djoser.email.ActivationEmail',
        'confirmation': 'djoser.email.ConfirmationEmail',
        'password_reset': 'djoser.email.PasswordResetEmail',
        'password_changed': 'djoser.email.PasswordChangedEmail',
        'username_reset': 'djoser.email.UsernameResetEmail',
        'username_changed': 'djoser.email.UsernameChangedEmail',
    },
}

# For development, print emails to console
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Simple JWT settings (if using JWT authentication)
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
    'ROTATE_REFRESH_TOKENS': True,
    'BLACKLIST_AFTER_ROTATION': True,
    'UPDATE_LAST_LOGIN': True,
}

# urls.py (in your project's root)

# from django.contrib import admin
# from django.urls import path, include

# urlpatterns = [
#     path('admin/', admin.site.urls),
#     path('auth/', include('djoser.urls')),
#     path('auth/', include('djoser.urls.jwt')), # For JWT authentication
#     # path('auth/', include('djoser.urls.authtoken')), # For Token authentication
#     # path('auth/', include('djoser.urls.social')), # For social authentication (if installed)
# ]

view raw JSON →