当维护模式打开时,django-maintenance-mode 显示 503 错误页面。
项目描述
django-维护模式
当维护模式打开时,django-maintenance-mode 显示 503 错误页面。
它适用于应用程序级别,因此您的 django 实例应该已启动。
它不使用数据库,也不会阻止数据库访问。
安装
- 运行
pip install django-maintenance-mode或下载 django-maintenance-mode并将maintenance_mode包添加到您的项目中 - 添加
maintenance_mode到settings.INSTALLED_APPS自定义应用程序之前 - 添加
maintenance_mode.middleware.MaintenanceModeMiddleware到settings.MIDDLEWARE最后一个中间件 - 添加您的自定义
templates/503.html文件 - 重启你的应用服务器
配置(可选)
设置
所有这些设置都是可选的,如果没有在settings.py默认值(下面列出)中定义将被使用。
# if True the maintenance-mode will be activated
MAINTENANCE_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) methods
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.LocalFileBackend"
# alternatively it is possible to use the default storage backend
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.DefaultStorageBackend"
# 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 writable
MAINTENANCE_MODE_STATE_FILE_PATH = "maintenance_mode_state.txt"
# if True admin site will not be affected by the maintenance-mode page
MAINTENANCE_MODE_IGNORE_ADMIN_SITE = False
# if True anonymous users will not see the maintenance-mode page
MAINTENANCE_MODE_IGNORE_ANONYMOUS_USER = False
# if True authenticated users will not see the maintenance-mode page
MAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER = False
# if True the staff will not see the maintenance-mode page
MAINTENANCE_MODE_IGNORE_STAFF = False
# if True the superuser will not see the maintenance-mode page
MAINTENANCE_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 objects
MAINTENANCE_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 = None
使用以下方法检索用户的真实 IP 地址django-ipware:
MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS = "ipware.ip.get_ip"
# list of urls that will not be affected by the maintenance-mode
# urls will be used to compile regular expressions objects
MAINTENANCE_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 use
MAINTENANCE_MODE_IGNORE_TESTS = False
# the absolute url where users will be redirected to during maintenance-mode
MAINTENANCE_MODE_REDIRECT_URL = None
# the template that will be shown by the maintenance-mode page
MAINTENANCE_MODE_TEMPLATE = "503.html"
# the path of the function that will return the template context -> 'myapp.mymodule.myfunction'
MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None
# the HTTP status code to send
MAINTENANCE_MODE_STATUS_CODE = 503
# the value in seconds of the Retry-After header during maintenance-mode
MAINTENANCE_MODE_RETRY_AFTER = 3600 # 1 hour
上下文处理器
如果要访问模板中的 maintenance_mode 状态,请将 maintenance_mode.context_processors.maintenance_mode添加到您的 context_processors 列表中。settings.py
TEMPLATES = [
{
# ...
"OPTIONS": {
"context_processors": [
# ...
"maintenance_mode.context_processors.maintenance_mode",
# ...
],
},
# ...
},
]
日志记录
您可以在启用维护模式时禁用通过电子邮件向管理员发送 503 错误:
LOGGING = {
"filters": {
"require_not_maintenance_mode_503": {
"()": "maintenance_mode.logging.RequireNotMaintenanceMode503",
},
...
},
"handlers": {
...
},
...
}
上下文管理器
您可以强制执行代码块在维护模式下运行或不使用上下文管理器:
from maintenance_mode.core import maintenance_mode_off, maintenance_mode_on
with maintenance_mode_on():
# do stuff
pass
with maintenance_mode_off():
# do stuff
pass
网址
如果您希望超级用户能够使用 url 设置 maintenance_mode,请将 maintenance_mode.urls添加到。urls.py
urlpatterns = [
# ...
url(r"^maintenance-mode/", include("maintenance_mode.urls")),
# ...
]
意见
您可以使用视图装饰器在视图级别强制打开/关闭维护模式:
from maintenance_mode.decorators import force_maintenance_mode_off, force_maintenance_mode_on
@force_maintenance_mode_off
def my_view_a(request):
# never return 503 response
pass
@force_maintenance_mode_on
def my_view_b(request):
# always return 503 response
pass
用法
Python
from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode
set_maintenance_mode(True)
if get_maintenance_mode():
set_maintenance_mode(False)
或者
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 your command(s)
call_command("maintenance_mode", "off")
模板
{% if maintenance_mode %}
<!-- html -->
{% endif %}
终端
跑python manage.py maintenance_mode <on|off>
(这不是 Heroku 友好的,因为heroku run 的任何执行都 manage.py 将在单独的工作人员测功机上运行,而不是在网络上运行。因此,状态文件设置但在错误的机器上。您应该使用自定义 MAINTENANCE_MODE_STATE_BACKEND。)
网址
超级用户可以使用以下 url 更改维护模式:
/maintenance-mode/off/
/maintenance-mode/on/
测试
# create python virtual environment
virtualenv testing_django_maintenance_mode
# activate virtualenv
cd testing_django_maintenance_mode && . bin/activate
# clone repo
git clone https://github.com/fabiocaccamo/django-maintenance-mode.git src && cd src
# install requirements
pip install -r requirements.txt
pip install -r requirements-test.txt
# run tests
tox
# or
python setup.py test
# or
python -m django test --settings "tests.settings"
执照
在MIT 许可下发布。
也可以看看
-
django-admin-interface- 由管理员自己定制的默认管理界面。弹出窗口替换为模态框。