'WSGIRequest' 从 Django 1.3 升级到 1.9 时对象没有属性 'session'
'WSGIRequest' object has no attribute 'session' while upgrading from django 1.3 to 1.9
类似这个问题'WSGIRequest' object has no attribute 'session'
但是我的中间件 类 的顺序是正确的。
INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.staticfiles',
'membership',
'treebeard',
'haystack',
'reversion',
]
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
我正在重定向到登录
url(r'^$', RedirectView.as_view(url='login/')),
url(r'^login/$', 'membership.views.loginView', name='login'),
然后
def loginView(request):
a = request.session
抛出错误
MIDDLEWARE
是 1.10 中的新设置,它将取代旧的 MIDDLEWARE_CLASSES
。
由于您当前使用的是 1.9,Django 无法识别 MIDDLEWARE
设置。您应该改用 MIDDLEWARE_CLASSES
设置:
MIDDLEWARE_CLASSES = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Django 2.0
您可以在 settings.py、MIDDLEWARE_CLASSES = [....]:
中试试这个
将MIDDLEWARE_CLASSES=[...]更改为MIDDLEWARE=[...]
从 MIDDLEWARE=[...] 列表中删除 SessionAuthenticationMiddleware。
The MIDDLEWARE_CLASSES setting is deprecated in Django 1.10, and
removed in Django 2.0.
The SessionAuthenticationMiddleware class is removed. It provided no
functionality since session authentication is unconditionally enabled
in Django 1.10.
检查中间件的顺序,如果你试图在会话中间件上面列出的某些中间件上访问它,你会得到这个错误。
当您输入错误时,也会抛出此错误。即
request.sesion ...
而不是
request.session ...
类似这个问题'WSGIRequest' object has no attribute 'session'
但是我的中间件 类 的顺序是正确的。
INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.staticfiles',
'membership',
'treebeard',
'haystack',
'reversion',
]
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
我正在重定向到登录
url(r'^$', RedirectView.as_view(url='login/')),
url(r'^login/$', 'membership.views.loginView', name='login'),
然后
def loginView(request):
a = request.session
抛出错误
MIDDLEWARE
是 1.10 中的新设置,它将取代旧的 MIDDLEWARE_CLASSES
。
由于您当前使用的是 1.9,Django 无法识别 MIDDLEWARE
设置。您应该改用 MIDDLEWARE_CLASSES
设置:
MIDDLEWARE_CLASSES = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Django 2.0
您可以在 settings.py、MIDDLEWARE_CLASSES = [....]:
中试试这个将MIDDLEWARE_CLASSES=[...]更改为MIDDLEWARE=[...]
从 MIDDLEWARE=[...] 列表中删除 SessionAuthenticationMiddleware。
The MIDDLEWARE_CLASSES setting is deprecated in Django 1.10, and removed in Django 2.0.
The SessionAuthenticationMiddleware class is removed. It provided no functionality since session authentication is unconditionally enabled in Django 1.10.
检查中间件的顺序,如果你试图在会话中间件上面列出的某些中间件上访问它,你会得到这个错误。
当您输入错误时,也会抛出此错误。即
request.sesion ...
而不是
request.session ...