將項目遷移至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項目中settings的中間件默認書寫格式為:
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創建一個新的項目, 中間件書寫格式為↓↓↓:
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.
修改中間件的書寫格式以及變量名即可解決此問題.
至於SessionAuthenticationMiddleware中間件,如果曾經有過,刪除即可
否則報錯:
AttributeError: module 'django.contrib.auth.middleware' has no attribute 'SessionAuthenticationMiddleware'
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.