Django 1.8 admin 产生'WSGIRequest' object has no attribute 'user'的错误

Posted ZSR0401

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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'的错误的主要内容,如果未能解决你的问题,请参考以下文章

从 1.8 升级到 1.9 Django Admin get_urls 不起作用

Django学习中遇见的问题总结

Django学习中遇见的问题总结

在 Django 1.8 中覆盖现有的模板标记方法

Django 1.9 admin 使用suit 小记

如何解决Django 1.8在migrate时失败