[英]'WSGIRequest' object has no attribute 'sessions' (Django 2.1.2)
I am working on a django web app and I need to use sessions and cookies. 我正在使用django网络应用程序,并且需要使用会话和cookie。 I wasn't getting this error before while using sessions and cookies but now It is happening again:
在使用会话和cookie之前,我没有收到此错误,但是现在又发生了:
AttributeError at /dashboard/
'WSGIRequest' object has no attribute 'sessions'
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/
Django Version: 2.1.2
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'sessions
...
def dashboard(request):
context = {
}
request.sessions.set_test_cookie()
request.sessions["test"] = "This Is A Test"
return HttpResponse(f'<h1>This Is Where The Dashboard is Going To Be :)</h1><br /><h1>Welcome</h1>')
I already checked my MIDDLEWARE order and made sure that MIDDLEWARE wasn't supposed to be MIDDLEWARE_CLASSES. 我已经检查了我的MIDDLEWARE订单,并确保不应将MIDDLEWARE设置为MIDDLEWARE_CLASSES。 Here is the code in my settings.py file:
这是我的settings.py文件中的代码:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'portalhome.apps.PortalhomeConfig',
'Users.apps.UsersConfig',
]
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',
]
ROOT_URLCONF = 'airquality.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'airquality.wsgi.application'
The wierd thing is is that django is able to set a csrf_token (a cookie) and before this error my sessions were working fine. 奇怪的是django能够设置一个csrf_token(一个cookie),在出现此错误之前,我的会话工作正常。 I also did not change any of the code from when the sessions were working it just stopped working.
从会话开始工作起,我也没有更改任何代码,只是停止工作。
You will have session as a property on request. 您可以根据要求将会话作为属性。 Try using
request.session
instead of request.sessions
. 尝试使用
request.session
而不是request.sessions
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.