Django project looking for "attribute '_session_cache'"
Asked Answered
C

7

22

So I have a Django project that doesn't use a database (the 'DATABASES' setting is commented out). I chose to use Django as there's a chance I will need the database functionality in the future. Anyway, I've been working on and off of tbhis project for a couple of months with no problems. I'm running Linux Mint and have had no troubles using the python manage.py runserver command so far.

Well, today I fired up the app and started the local server with no problems. I then tried to open the app in my browser and received the rather hideous error message:

Traceback (most recent call last):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/base.py", line 170, in _get_session
    return self._session_cache
AttributeError: 'SessionStore' object has no attribute '_session_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/core/handlers/base.py", line 87, in get_response
    response = middleware_method(request)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/middleware.py", line 34, in process_request
    if user and hasattr(user, 'get_session_auth_hash'):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/utils/functional.py", line 224, in inner
    self._setup()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/utils/functional.py", line 357, in _setup
    self._wrapped = self._setupfunc()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/middleware.py", line 23, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/middleware.py", line 11, in get_user
    request._cached_user = auth.get_user(request)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/__init__.py", line 151, in get_user
    user_id = request.session[SESSION_KEY]
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/base.py", line 49, in __getitem__
    return self._session[key]
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/base.py", line 175, in _get_session
    self._session_cache = self.load()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/db.py", line 21, in load
    expire_date__gt=timezone.now()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 351, in get
    num = len(clone)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 775, in execute_sql
    sql, params = self.as_sql()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 100, in as_sql
    out_cols, s_params = self.get_columns(with_col_aliases)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 246, in get_columns
    col_aliases)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 328, in get_default_columns
    r = '%s.%s' % (qn(alias), qn2(column))
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 62, in __call__
    r = self.connection.ops.quote_name(name)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/backends/dummy/base.py", line 18, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/base.py", line 170, in _get_session
    return self._session_cache
AttributeError: 'SessionStore' object has no attribute '_session_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.3/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/staticfiles/handlers.py", line 64, in __call__
    return self.application(environ, start_response)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
    response = self.get_response(request)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/core/handlers/base.py", line 199, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/core/handlers/base.py", line 236, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/views/debug.py", line 91, in technical_500_response
    html = reporter.get_traceback_html()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/views/debug.py", line 349, in get_traceback_html
    c = Context(self.get_traceback_data(), use_l10n=False)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/views/debug.py", line 307, in get_traceback_data
    frames = self.get_traceback_frames()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/views/debug.py", line 465, in get_traceback_frames
    'vars': self.filter.get_traceback_frame_variables(self.request, tb.tb_frame),
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/views/debug.py", line 232, in get_traceback_frame_variables
    cleansed[name] = self.cleanse_special_types(request, value)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/views/debug.py", line 187, in cleanse_special_types
    if isinstance(value, HttpRequest):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/utils/functional.py", line 224, in inner
    self._setup()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/utils/functional.py", line 357, in _setup
    self._wrapped = self._setupfunc()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/middleware.py", line 23, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/middleware.py", line 11, in get_user
    request._cached_user = auth.get_user(request)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/auth/__init__.py", line 151, in get_user
    user_id = request.session[SESSION_KEY]
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/base.py", line 49, in __getitem__
    return self._session[key]
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/base.py", line 175, in _get_session
    self._session_cache = self.load()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/contrib/sessions/backends/db.py", line 21, in load
    expire_date__gt=timezone.now()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 351, in get
    num = len(clone)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 775, in execute_sql
    sql, params = self.as_sql()
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 100, in as_sql
    out_cols, s_params = self.get_columns(with_col_aliases)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 246, in get_columns
    col_aliases)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 328, in get_default_columns
    r = '%s.%s' % (qn(alias), qn2(column))
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 62, in __call__
    r = self.connection.ops.quote_name(name)
  File "/home/peter/.virtualenvs/vis_it/lib/python3.3/site-packages/django/db/backends/dummy/base.py", line 18, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
[28/Nov/2014 13:18:54] "GET / HTTP/1.1" 500 59

I've not touched the app since I last worked on it and it was working fine then. I really have no idea what this is talking about as I have no caches implemented and am not using a database... I've asked a few colleges who are familiar with Django and have no idea what this is either. Any Ideas? I've also tried setting DATABASES to an empty dict {} on the advice of a post online but to no effect. At a bit of a loss.

EDIT: Thought I should mention that in the interim since I last touched this project, I've since started and set up a new Django project that does use a database. Is it possible that this project is somehow cached and breaking the one I'm currently trying to work on?

EDIT2: I should point out that this webapp is currently live and working at http://mrcagney-maps.com. The code is exactly the same (having not touched it since I last pushed to the server). Really weird.

Classic answered 28/11, 2014 at 0:25 Comment(0)
L
7

The error AttributeError: 'SessionStore' object has no attribute '_session_cache' can stem from the database not having a django_session table. However, since you are not using a table, you would need to make sure that you don't have the 'django.contrib.sessions.middleware.SessionMiddleware' in your MIDDLEWARE_CLASSES in the project's settings file. If it is in there, it will look for a database table which stores the sessions, causing the above error.

L answered 28/11, 2014 at 4:17 Comment(4)
Hey thanks for that. I'll give that a go on Monday when I get back to my machine but have a feeling I tried that and received a different error regarding the missing middleware (even though I'm not using sessions). The strange thing is, this app is currently running on the live server fine, both with the DATABASES setting commented out and that middleware class included. Really weird. I don't understand how it ever worked online... yet it has been and currently is...Classic
UPDATE: Ok so you were right about the middleware. To get it to work again I had to remove the following: 'django.contrib.sessions.middleware.SessionMiddleware' 'django.contrib.auth.middleware.AuthenticationMiddleware' 'django.contrib.auth.middleware.SessionAuthenticationMiddleware' 'django.contrib.messages.middleware.MessageMiddleware' No idea why they were in there to be honest. Probably left over from a previous project I was basing it offClassic
I also still don't understand why including them didn't break the version on the live server but did on my local machine. I think it has something to do with Django's python manage.py runserver command caching something between projects. The project I was previously working on has user authentication and sessions etc and I believe some remnant of running that app locally was breaking my other app. Something like caching the user locally and it still looked for that user/session when switching apps. That's all I can think of. Anyway. It's working now so I've accepted your answer. Cheers.Classic
My best guess is that you may have overridden the middleware in production. Just a guess. It should have exploded there as well. Good to know I helped out!L
E
53

For future Googlers - I ran into this issue and the above solutions did not work for me. What did work for me was clearing/deleting my cookies in Chrome for the 127.0.0.1 URL. So go to Settings or press CMD+, then Cookies and other site data, then find 127.0.0.1 or localhost and delete those cookies. Refresh the local dev host page and the error should be gone. This has something to do with a corrupted session / cookie file.

Engine answered 25/8, 2020 at 2:42 Comment(4)
Actual life saver- this probably saved me more than a couple hours. Wish I could upvote again.Rain
Will this impact prod? Or is this a chrome localhost thing?Jeer
@MichaelDavidson this is a chrome localhost thing. deleting the corrupted cookie will not affect production.Engine
omg thank you so much, tried so many sessions like adding and removing recent apps i installed and still had the error, this worked thank you.Danelle
G
13

I'm not sure why I started to get this error, it involved an upgrade though. I just deleted all the sessions and after logging back in all was well.

# from the shell but equivalent sql would work fine too
from django.contrib.sessions.models import Session
Session.objects.all().delete()
Glacier answered 7/12, 2020 at 17:57 Comment(1)
This solved my issue. I had changed django's secret key, thus invalidating the present sessions.Ringtailed
L
7

The error AttributeError: 'SessionStore' object has no attribute '_session_cache' can stem from the database not having a django_session table. However, since you are not using a table, you would need to make sure that you don't have the 'django.contrib.sessions.middleware.SessionMiddleware' in your MIDDLEWARE_CLASSES in the project's settings file. If it is in there, it will look for a database table which stores the sessions, causing the above error.

L answered 28/11, 2014 at 4:17 Comment(4)
Hey thanks for that. I'll give that a go on Monday when I get back to my machine but have a feeling I tried that and received a different error regarding the missing middleware (even though I'm not using sessions). The strange thing is, this app is currently running on the live server fine, both with the DATABASES setting commented out and that middleware class included. Really weird. I don't understand how it ever worked online... yet it has been and currently is...Classic
UPDATE: Ok so you were right about the middleware. To get it to work again I had to remove the following: 'django.contrib.sessions.middleware.SessionMiddleware' 'django.contrib.auth.middleware.AuthenticationMiddleware' 'django.contrib.auth.middleware.SessionAuthenticationMiddleware' 'django.contrib.messages.middleware.MessageMiddleware' No idea why they were in there to be honest. Probably left over from a previous project I was basing it offClassic
I also still don't understand why including them didn't break the version on the live server but did on my local machine. I think it has something to do with Django's python manage.py runserver command caching something between projects. The project I was previously working on has user authentication and sessions etc and I believe some remnant of running that app locally was breaking my other app. Something like caching the user locally and it still looked for that user/session when switching apps. That's all I can think of. Anyway. It's working now so I've accepted your answer. Cheers.Classic
My best guess is that you may have overridden the middleware in production. Just a guess. It should have exploded there as well. Good to know I helped out!L
J
3

Here is what worked for me. Since there is no databases in your application. Admin page looks for the database be it default. So first lets create the default databases.

Shut down your servers and run

python manage.py makemigrations
python manage.py migrate

Now create the admin or superuser for your application. Fill username and password.

python manage.py createsuperuser

Now restart your server and go the admin page

python manage.py runserver
Jayson answered 21/5, 2019 at 4:24 Comment(2)
I renamed the project, and the answer is working for me.Gielgud
Thanks a lot.i had the same problem and it worked for meMarchant
T
0

Throwed the same error for me after upgraded my wagtail version... So, for me worked even simpler solution or u didn't noticed already :D Actually I opened the browser > F12 > Storage Tab and Delete all Cookies and all other Cached Data

Tetragon answered 24/12, 2020 at 23:42 Comment(1)
A good way to nuke the problem. Deleting the session cookie for the site is adequate and can be done either using the browser developer tools or a handy plugin like EditThisCookie.Contralto
A
0

Users facing this issue via Postman please make sure that the value in the request header for Cache-Control is no-cache.

If not then clear the cookie data for the domain.

Postman Screenshot

Arthro answered 13/4, 2021 at 15:48 Comment(0)
C
0

In normal case when we use

from django.http import JsonResponse

for sending response we have no need to uses sessions.

But when we use some package such as djangorestframework we force to use sessions.

Sessions need storage place in sqllite,mysql ,...or others then we need run these commands:

python manage.py makemigrations
python manage.py migrate
Cat answered 27/8, 2021 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.