码迷,mamicode.com
首页 > 其他好文 > 详细

Django2.X 中间件兼容 书写格式

时间:2019-09-29 20:16:40      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:ram   ror   messages   col   admin   options   class   除了   mon   

将项目迁移至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


修改书写格式以及变量名即可.

Django2.X 中间件兼容 书写格式

标签:ram   ror   messages   col   admin   options   class   除了   mon   

原文地址:https://www.cnblogs.com/jrri/p/11609438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!