'WSGIRequest' 对象没有属性 'sessions' (Django 2.1.2)

'WSGIRequest' object has no attribute 'sessions' (Django 2.1.2)

我正在开发一个 django 网络应用程序,我需要使用会话和 cookie。我以前在使用会话和 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>')

我已经检查了我的 MIDDLEWARE 订单并确保 MIDDLEWARE 不应该是 MIDDLEWARE_CLASSES。这是我的 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'

奇怪的是 django 能够设置一个 csrf_token(一个 cookie)并且在这个错误之前我的会话工作正常。我也没有更改任何代码,因为会话正在工作时它只是停止工作。

您将根据要求将 session 作为 属性。尝试使用 request.session 而不是 request.sessions.