django-maintenance-mode

0.22.0 · active · verified Thu Apr 16

django-maintenance-mode is a middleware that shows a 503 error page to users when maintenance mode is active in a Django project. The current version is 0.22.0. It maintains an active release cadence, frequently updating to support new Python and Django versions.

Common errors

Warnings

Install

Imports

Quickstart

To enable maintenance mode, add `maintenance_mode` to your `INSTALLED_APPS` and `maintenance_mode.middleware.MaintenanceModeMiddleware` to your `MIDDLEWARE`. You can activate maintenance mode by setting `MAINTENANCE_MODE = True` in your settings or by using the `python manage.py maintenance_mode on` command. Configure exceptions for URLs, staff users, or specific IP addresses using settings like `MAINTENANCE_MODE_EXCEPT_URLS` and `MAINTENANCE_MODE_EXCEPT_STAFF`.

import os

# settings.py
INSTALLED_APPS = [
    # ... other apps
    'maintenance_mode',
]

MIDDLEWARE = [
    # ... other middleware (e.g., SessionMiddleware, CommonMiddleware)
    'maintenance_mode.middleware.MaintenanceModeMiddleware',
    # Place above other middleware that might serve content (e.g., WhiteNoise)
    # Place below any middleware that identifies users if you want staff to bypass
]

# Activate maintenance mode either via settings or manage.py command
MAINTENANCE_MODE = os.environ.get('DJANGO_MAINTENANCE_MODE', 'False').lower() == 'true'

# Optional: Customize behavior
MAINTENANCE_MODE_EXCEPT_URLS = (
    r'^/admin/?$', # Allow admin access
)
MAINTENANCE_MODE_EXCEPT_STAFF = True # Allow logged-in staff users
MAINTENANCE_MODE_REDIRECT_URL = '/503/' # Redirect to a specific URL
MAINTENANCE_MODE_TEMPLATE = '503.html' # Use a custom template

# In your project's urls.py (if using redirect or custom template path)
# from django.urls import path
# from django.views.generic import TemplateView
# urlpatterns = [
#     path('503/', TemplateView.as_view(template_name='503.html'), name='maintenance_503'),
# ]

# To toggle maintenance mode from the command line:
# python manage.py maintenance_mode on
# python manage.py maintenance_mode off

view raw JSON →