您应该将生产设置和本地设置拆分为不同的文件,然后在本地设置中添加中间件。帮助您入门的小示例:
文件结构:
Settings
---> __init__.py
---> prod.py
---> dev.py
示例如何仅在 dev.py 设置中添加 django_debug_toolbar:
__init__ .py:
# Other settings ommitted
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
开发者.py:
from settings import *
# Other settings ommitted
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)