'WSGIRequest' object has no attribute 'session' while upgrading from django 1.3 to 1.9

Shawn Anderson picture Shawn Anderson · Sep 14, 2016 · Viewed 16.5k times · Source

Similar to this question 'WSGIRequest' object has no attribute 'session'

But my MIDDLEWARE classes are in the correct order.

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',
]

I am redirecting to login

url(r'^$',  RedirectView.as_view(url='login/')),
url(r'^login/$', 'membership.views.loginView', name='login'),

and then

def loginView(request):
    a = request.session

Throws the error

Answer

knbk picture knbk · Sep 14, 2016

MIDDLEWARE is a new setting in 1.10 that will replace the old MIDDLEWARE_CLASSES.

Since you're currently on 1.9, Django doesn't recognize the MIDDLEWARE setting. You should use the MIDDLEWARE_CLASSES setting instead:

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',
]