django-maintenance-mode
django-maintenance-mode shows a 503 error page when maintenance-mode is on.
It works at application level, so your django instance should be up.
It doesn't use database and doesn't prevent database access.
Installation
- Run
pip install django-maintenance-mode
or download django-maintenance-mode and add the maintenance_mode package to your project - Add
maintenance_mode
to settings.INSTALLED_APPS
before custom applications - Add
maintenance_mode.middleware.MaintenanceModeMiddleware
to settings.MIDDLEWARE
as last middleware - Add your custom
templates/503.html
file - Restart your application server
Configuration (optional)
Settings
All these settings are optional, if not defined in settings.py
the default values (listed below) will be used.
MAINTENANCE_MODE = None
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.LocalFileBackend"
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.DefaultStorageBackend"
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.StaticStorageBackend"
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.CacheBackend"
MAINTENANCE_MODE_STATE_BACKEND_FALLBACK_VALUE = False
MAINTENANCE_MODE_STATE_FILE_PATH = "maintenance_mode_state.txt"
MAINTENANCE_MODE_IGNORE_ADMIN_SITE = False
MAINTENANCE_MODE_IGNORE_ANONYMOUS_USER = False
MAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER = False
MAINTENANCE_MODE_IGNORE_STAFF = False
MAINTENANCE_MODE_IGNORE_SUPERUSER = False
MAINTENANCE_MODE_IGNORE_IP_ADDRESSES = ()
MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS = None
Retrieve user's real IP address using django-ipware
:
MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS = "ipware.ip.get_ip"
MAINTENANCE_MODE_GET_CONTEXT = None
MAINTENANCE_MODE_IGNORE_URLS = ()
MAINTENANCE_MODE_IGNORE_TESTS = False
MAINTENANCE_MODE_LOGOUT_AUTHENTICATED_USER = False
MAINTENANCE_MODE_REDIRECT_URL = None
MAINTENANCE_MODE_RESPONSE_TYPE = "html"
MAINTENANCE_MODE_TEMPLATE = "503.html"
MAINTENANCE_MODE_STATUS_CODE = 503
MAINTENANCE_MODE_RETRY_AFTER = 3600
Context Processors
Add maintenance_mode.context_processors.maintenance_mode to your context_processors list in settings.py
if you want to access the maintenance_mode status in your templates.
TEMPLATES = [
{
"OPTIONS": {
"context_processors": [
"maintenance_mode.context_processors.maintenance_mode",
],
},
},
]
Logging
You can disable emailing 503 errors to admins while maintenance mode is enabled:
LOGGING = {
"filters": {
"require_not_maintenance_mode_503": {
"()": "maintenance_mode.logging.RequireNotMaintenanceMode503",
},
...
},
"handlers": {
...
},
...
}
Context Managers
You can force a block of code execution to run under maintenance mode or not using context managers:
from maintenance_mode.core import maintenance_mode_off, maintenance_mode_on
with maintenance_mode_on():
pass
with maintenance_mode_off():
pass
URLs
Add maintenance_mode.urls to urls.py
if you want superusers able to set maintenance_mode using urls.
urlpatterns = [
re_path(r"^maintenance-mode/", include("maintenance_mode.urls")),
]
Views
You can force maintenance mode on/off at view level using view decorators:
Function-based views
from maintenance_mode.decorators import force_maintenance_mode_off, force_maintenance_mode_on
@force_maintenance_mode_off
def my_view_a(request):
pass
@force_maintenance_mode_on
def my_view_b(request):
pass
Class-based views
from maintenance_mode.decorators import force_maintenance_mode_off, force_maintenance_mode_on
urlpatterns = [
path("", force_maintenance_mode_off(YourView.as_view()), name="my_view"),
path("", force_maintenance_mode_on(YourView.as_view()), name="my_view"),
]
Usage
Python
from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode
set_maintenance_mode(True)
if get_maintenance_mode():
set_maintenance_mode(False)
or
from django.core.management import call_command
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, *args, **options):
call_command("maintenance_mode", "on")
call_command("maintenance_mode", "off")
Templates
{% if maintenance_mode %}
{% endif %}
Terminal
Run python manage.py maintenance_mode <on|off>
(This is not Heroku-friendly because any execution of heroku run manage.py
will be run on a separate worker dyno, not the web one. Therefore the state-file is set but on the wrong machine. You should use a custom MAINTENANCE_MODE_STATE_BACKEND
.)
URLs
Superusers can change maintenance-mode using the following urls:
/maintenance-mode/off/
/maintenance-mode/on/
Testing
git clone https://github.com/fabiocaccamo/django-maintenance-mode.git && cd django-maintenance-mode
python -m venv venv && . venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-test.txt
pre-commit install --install-hooks
tox
python runtests.py
python -m django test --settings "tests.settings"
License
Released under MIT License.
Supporting
- :star: Star this project on GitHub
- :octocat: Follow me on GitHub
- :blue_heart: Follow me on Twitter
- :moneybag: Sponsor me on Github
See also
-
django-admin-interface
- the default admin interface made customizable by the admin itself. popup windows replaced by modals. 🧙 ⚡
-
django-colorfield
- simple color field for models with a nice color-picker in the admin. 🎨
-
django-extra-settings
- config and manage typed extra settings using just the django admin. ⚙️
-
django-redirects
- redirects with full control. ↪️
-
django-treenode
- probably the best abstract model / admin for your tree based stuff. 🌳
-
python-benedict
- dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘
-
python-codicefiscale
- encode/decode Italian fiscal codes - codifica/decodifica del Codice Fiscale. 🇮🇹 💳
-
python-fontbro
- friendly font operations. 🧢
-
python-fsutil
- file-system utilities for lazy devs. 🧟♂️