Django2.X middleware中間件兼容 書寫格式

將項目遷移至django2.X, 中間件提示錯誤爲:

ERRORS:
?: (admin.E408) 'django.contrib.auth.middleware.AuthenticationMiddleware' must be in MIDDLEWARE in order to use the admin application.
?: (admin.E409) 'django.contrib.messages.middleware.MessageMiddleware' must be in MIDDLEWARE in order to use the admin application.
?: (admin.E410) 'django.contrib.sessions.middleware.SessionMiddleware' must be in MIDDLEWARE in order to use the admin application.django

 

解決方法:session

修改中間件的書寫格式以及變量名便可.app



在以往django項目中settings的中間件默認書寫格式爲:spa

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)


而使用新版Django建立一個新的項目, 中間件書寫格式↓↓↓: code

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


能夠看到新版django修改了中間件的書寫格式: 由元組轉變爲列表, 並移除了一箇中間件, SessionAuthenticationMiddleware.csrf


修改中間件的書寫格式以及變量名便可解決此問題.中間件

 

至於SessionAuthenticationMiddleware中間件,若是曾經有過,刪除便可blog

不然報錯:it

AttributeError: module 'django.contrib.auth.middleware' has no attribute 'SessionAuthenticationMiddleware'
io

The above exception was the direct cause of the following exception:

...

...

...

django.core.exceptions.ImproperlyConfigured: WSGI application 'yourproject.wsgi.application' could not be loaded; Error importing module.

相關文章
相關標籤/搜索