我收到此错误' WSGIRequest'对象在manage.py runserver上没有属性' session' 。当我尝试从request.session中检索会话dict时。
对此项目的搜索说我需要放入' django.contrib.sessions.middleware.SessionMiddleware'在MIDDLEWARE设置之上。然而,这似乎不起作用。
我的settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@ta=3ful!*bzj^o@3+avt#qrm9@uz%6ur_d@ihs#j--5us*r_('
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.sessions',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.staticfiles',
'FEM',
]
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'laizen.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',
#'django.core.context_processors.csrf',
],
},
},
]
WSGI_APPLICATION = 'laizen.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Amsterdam'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
os.path.join(BASE_DIR, 'FEM', 'static')
)
#STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
我的views.py函数:
def new_framework(request):
print(request.session)
return render(request, 'FEM/new_framework.html', {'bool_view_el': 'false'})
Django错误:
AttributeError at /new_framework/
'WSGIRequest' object has no attribute 'session'
Request Method: GET
Request URL: http://127.0.0.1:8000/new_framework/
Django Version: 1.9.6
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'session'
Exception Location: C:\Users\vik\Dropbox\Code\Web_Development\FEM_web_test\FEM\views.py in new_framework, line 23
Python Executable: C:\Anaconda3\python.exe
Python Version: 3.5.2
Python Path:
['C:\\Users\\vik\\Dropbox\\Code\\Web_Development\\FEM_web_test',
'C:\\Anaconda3\\python35.zip',
'C:\\Anaconda3\\DLLs',
'C:\\Anaconda3\\lib',
'C:\\Anaconda3',
'C:\\Anaconda3\\lib\\site-packages',
'C:\\Anaconda3\\lib\\site-packages\\Sphinx-1.3.5-py3.5.egg',
'C:\\Anaconda3\\lib\\site-packages\\cryptography-1.0.2-py3.5-win-amd64.egg',
'C:\\Anaconda3\\lib\\site-packages\\win32',
'C:\\Anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Anaconda3\\lib\\site-packages\\Pythonwin',
'C:\\Anaconda3\\lib\\site-packages\\setuptools-20.7.0-py3.5.egg']
Server time: Sun, 28 Aug 2016 12:53:18 +0200
答案 0 :(得分:0)
您使用的是Django 1.9版; MIDDLEWARE设置仅在1.10版中引入。对于像您这样的早期版本,您应该使用MIDDLEWARE_CLASSES - 或升级到1.10。