首页 > 代码库 > Django 1.8 admin 产生'WSGIRequest' object has no attribute 'user'的错误
Django 1.8 admin 产生'WSGIRequest' object has no attribute 'user'的错误
配置admin时,产生‘WSGIRequest‘ object has no attribute ‘user‘的错误
google了下,说是MIDDLEWARE配置的有问题,顺序应该保持参见(http://stackoverflow.com/questions/26576192/wsgirequest-object-has-no-attribute-user):
‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘,
但是这样设置之后,依然没有解决。
我的MIDDLEWARE的配置是:
MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware‘,
‘django.middleware.common.CommonMiddleware‘,
‘django.contrib.sessions.middleware.SessionMiddleware‘,
‘django.middleware.csrf.CsrfViewMiddleware‘,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘,
‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
‘django.contrib.messages.middleware.MessageMiddleware‘,
‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,
]
实际上,这是Django版本的问题,1.9之前,中间件的key为MIDDLEWARE_CLASSES, 1.9之后,为MIDDLEWARE。所以在开发环境和其他环境的版本不一致时,要特别小心,会有坑。
改配置为:
MIDDLEWARE_CLASSES = [
‘django.middleware.security.SecurityMiddleware‘,
‘django.middleware.common.CommonMiddleware‘,
‘django.contrib.sessions.middleware.SessionMiddleware‘,
‘django.middleware.csrf.CsrfViewMiddleware‘,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘,
‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
‘django.contrib.messages.middleware.MessageMiddleware‘,
‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,
]
问题解决。
参考 http://www.ziqiangxuetang.com/django/django-middleware.html
Django 1.8 admin 产生'WSGIRequest' object has no attribute 'user'的错误
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。