Package: django-maintenance-mode
django-maintenance-modedjango-maintenance-mode shows a 503 error page whenmaintenance-modeison.It works at application level, so your django instance should be up.It doesn't use database and doesn't prevent database access.InstallationRunpip install django-maintenance-modeordownload django-maintenance-modeand add themaintenance_modepackage to your projectAddmaintenance_modetosettings.INSTALLED_APPSbefore custom applicationsAddmaintenance_mode.middleware.MaintenanceModeMiddlewaretosettings.MIDDLEWAREas last middlewareAdd your customtemplates/503.htmlfileRestart your application serverConfiguration (optional)SettingsAll these settings are optional, if not defined insettings.pythe default values (listed below) will be used.# if True the maintenance-mode will be activatedMAINTENANCE_MODE=None# by default, to get/set the state value a local file backend is used# if you want to use the db or cache, you can create a custom backend# custom backends must extend 'maintenance_mode.backends.AbstractStateBackend' class# and implement get_value(self) and set_value(self, val) methodsMAINTENANCE_MODE_STATE_BACKEND="maintenance_mode.backends.LocalFileBackend"# alternatively it is possible to use the default storage backendMAINTENANCE_MODE_STATE_BACKEND="maintenance_mode.backends.DefaultStorageBackend"# alternatively it is possible to use the static storage backend# make sure that STATIC_ROOT and STATIC_URL are also setMAINTENANCE_MODE_STATE_BACKEND="maintenance_mode.backends.StaticStorageBackend"# alternatively it is possible to use the cache backend# you can use a custom cache backend by adding a `maintenance_mode` entry to `settings.CACHES`,# otherwise the default cache backend will be used.MAINTENANCE_MODE_STATE_BACKEND="maintenance_mode.backends.CacheBackend"# the fallback value that backends will return in case of failure# (actually this is only used by "maintenance_mode.backends.CacheBackend")MAINTENANCE_MODE_STATE_BACKEND_FALLBACK_VALUE=False# by default, a file named "maintenance_mode_state.txt" will be created in the settings.py directory# you can customize the state file path in case the default one is not writableMAINTENANCE_MODE_STATE_FILE_PATH="maintenance_mode_state.txt"# if True admin site will not be affected by the maintenance-mode pageMAINTENANCE_MODE_IGNORE_ADMIN_SITE=False# if True anonymous users will not see the maintenance-mode pageMAINTENANCE_MODE_IGNORE_ANONYMOUS_USER=False# if True authenticated users will not see the maintenance-mode pageMAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER=False# if True the staff will not see the maintenance-mode pageMAINTENANCE_MODE_IGNORE_STAFF=False# if True the superuser will not see the maintenance-mode pageMAINTENANCE_MODE_IGNORE_SUPERUSER=False# list of ip-addresses that will not be affected by the maintenance-mode# ip-addresses will be used to compile regular expressions objectsMAINTENANCE_MODE_IGNORE_IP_ADDRESSES=()# the path of the function that will return the client IP address given the request object -> 'myapp.mymodule.myfunction'# the default function ('maintenance_mode.utils.get_client_ip_address') returns request.META['REMOTE_ADDR']# in some cases the default function returns None, to avoid this scenario just use 'django-ipware'MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS=NoneRetrieve user's real IP address usingdjango-ipware:MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS="ipware.ip.get_ip"# the path of the function that will return the response context -> 'myapp.mymodule.myfunction'MAINTENANCE_MODE_GET_CONTEXT=None# list of urls that will not be affected by the maintenance-mode# urls will be used to compile regular expressions objectsMAINTENANCE_MODE_IGNORE_URLS=()# if True the maintenance mode will not return 503 response while running tests# useful for running tests while maintenance mode is on, before opening the site to public useMAINTENANCE_MODE_IGNORE_TESTS=False# if True authenticated users will be logged out from their current sessionMAINTENANCE_MODE_LOGOUT_AUTHENTICATED_USER=False# the absolute url where users will be redirected to during maintenance-modeMAINTENANCE_MODE_REDIRECT_URL=None# the type of the response returned during maintenance mode, can be either "html" or "json"MAINTENANCE_MODE_RESPONSE_TYPE="html"# the template that will be shown by the maintenance-mode pageMAINTENANCE_MODE_TEMPLATE="503.html"# the HTTP status code to sendMAINTENANCE_MODE_STATUS_CODE=503# the value in seconds of the Retry-After header during maintenance-modeMAINTENANCE_MODE_RETRY_AFTER=3600# 1 hourContext ProcessorsAddmaintenance_mode.context_processors.maintenance_modeto your context_processors list insettings.pyif you want to access the maintenance_mode status in your templates.TEMPLATES=[{# ..."OPTIONS":{"context_processors":[# ..."maintenance_mode.context_processors.maintenance_mode",# ...],},# ...},]LoggingYou 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 ManagersYou can force a block of code execution to run under maintenance mode or not using context managers:frommaintenance_mode.coreimportmaintenance_mode_off,maintenance_mode_onwithmaintenance_mode_on():# do stuffpasswithmaintenance_mode_off():# do stuffpassURLsAddmaintenance_mode.urlstourls.pyif you want superusers able to set maintenance_mode using urls.urlpatterns=[# ...re_path(r"^maintenance-mode/",include("maintenance_mode.urls")),# ...]ViewsYou can force maintenance mode on/off at view level using view decorators:Function-based viewsfrommaintenance_mode.decoratorsimportforce_maintenance_mode_off,force_maintenance_mode_on@force_maintenance_mode_offdefmy_view_a(request):# never return 503 responsepass@force_maintenance_mode_ondefmy_view_b(request):# always return 503 responsepassClass-based viewsfrommaintenance_mode.decoratorsimportforce_maintenance_mode_off,force_maintenance_mode_onurlpatterns=[# never return 503 responsepath("",force_maintenance_mode_off(YourView.as_view()),name="my_view"),# always return 503 responsepath("",force_maintenance_mode_on(YourView.as_view()),name="my_view"),]UsagePythonfrommaintenance_mode.coreimportget_maintenance_mode,set_maintenance_modeset_maintenance_mode(True)ifget_maintenance_mode():set_maintenance_mode(False)orfromdjango.core.managementimportcall_commandfromdjango.core.management.baseimportBaseCommandclassCommand(BaseCommand):defhandle(self,*args,**options):call_command("maintenance_mode","on")# call your command(s)call_command("maintenance_mode","off")Templates{% if maintenance_mode %}<!-- html -->{% endif %}TerminalRunpython manage.py maintenance_mode <on|off>(This is not Heroku-friendly becauseany execution of heroku runmanage.pywill be run on a separate worker dyno, not the web one. Thereforethe state-file is set but on the wrong machine. You should use a customMAINTENANCE_MODE_STATE_BACKEND.)URLsSuperusers can change maintenance-mode using the following urls:/maintenance-mode/off//maintenance-mode/on/Testing# clone repositorygitclonehttps://github.com/fabiocaccamo/django-maintenance-mode.git&&cddjango-maintenance-mode# create virtualenv and activate itpython-mvenvvenv&&.venv/bin/activate# upgrade pippython-mpipinstall--upgradepip# install requirementspipinstall-rrequirements.txt-rrequirements-test.txt# install pre-commit to run formatters and linterspre-commitinstall--install-hooks# run teststox# orpythonruntests.py# orpython-mdjangotest--settings"tests.settings"LicenseReleased underMIT License.Supporting:star: Star this project onGitHub:octocat: Follow me onGitHub:blue_heart: Follow me onTwitter:moneybag: Sponsor me onGithubSee alsodjango-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. 🧟‍♂️