• 将项目迁移至django更高的版本, 提示408、409、410中间件错误的解决办法


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

    解决方法:

    修改中间件的书写格式以及变量名即可.

    注意:变量名从MIDDLEWARE_CLASSES变成了MIDDLEWARE

    在以往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.

  • 相关阅读:
    JS提取子字符串函数比较
    js事件定义方式和获取事件对象event总结
    让body的clientHeight与html的clientHeight相等的方法
    关于原型链和继承问题的思考:为什么不能直接把父类的prototype赋值给子类的prototype
    [javascript权威指南笔记02]Throw语句和异常处理机制try/catch/finally
    转载:javascript语句标签
    转:JS中强大的正则表达式
    分享我常用的Javascript工具函数
    对prototype,instanceof和constrctor的理解
    xml基础知识总结和回顾
  • 原文地址:https://www.cnblogs.com/hongdoudou/p/12637509.html
Copyright © 2020-2023  润新知