Question

So I was re-factoring my code (because it was so ugly I could not leave with it). And without touching the settings.py the SessionMiddleware stopped existing for Django.

This traceback is after I removed any other middleware that needed sessions. It happens whenever I call a function that tries to access session from the request (as you can read).

AttributeError: 'WSGIRequest' object has no attribute 'session'

Before that it was

AssertionError: The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'.

And here is what I have in my settings.py regarding sessions:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    # 'django.contrib.auth.middleware.AuthenticationMiddleware',
    # 'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

[...]

INSTALLED_APPS += ('django.contrib.sessions',)

# SESSION_ENGINE = 'django.contrib.sessions.backends.db'
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
SESSION_SAVE_EVERY_REQUEST = True

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake'
    },
    'files_downloads': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake'
    }
}
CACHE_BACKEND = 'locmem://'

Has anyone any idea of what is happening to my server?

EDIT: I have found many similar questions on StackOverflow (and a few that are almost copies of mine) and I have tried them. Any thing I am not seeing here?

Was it helpful?

Solution

Since I believe this is of interests to many new Django developers out there, I will tell how I solved it as an answer.

I solved this by uninstalling and reinstalling Django.

Apparently after uninstalling some unnecessary packages related to Django the session middleware "disappeared" or was corrupted (still trying to work it out here). But it turns out it was fixed with a fresh Django installation (since it is a default Django middleware).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top