Django admin AttributeError: 'WSGIRequest' object has no attribute 'user' 错误

在调用django的admin管理后台时,报了错误:

AttributeError: 'WSGIRequest' object has no attribute 'user'

在网上搜索后发现是django版本问题,在django 1.10 之后,settings.py文件的中间件统一更新为了 MIDDLEWARE,而在之前的版本是 MIDDLEWARE_CLASSES。

old-style:

MIDDLEWARE_CLASSES = [
   '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',
]

new-style

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',
]

django1.10文档地址:https://docs.djangoproject.com/en/stable/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware

你可能感兴趣的:(Django admin AttributeError: 'WSGIRequest' object has no attribute 'user' 错误)