寻找“属性'_session_cache'”的Django项目

Posted

技术标签:

【中文标题】寻找“属性\'_session_cache\'”的Django项目【英文标题】:Django project looking for "attribute '_session_cache'"寻找“属性'_session_cache'”的Django项目 【发布时间】:2015-01-26 14:56:48 【问题描述】:

所以我有一个不使用数据库的 Django 项目(“DATABASES”设置已被注释掉)。我选择使用 Django,因为将来我可能需要数据库功能。无论如何,我已经在这个项目上工作了几个月,没有任何问题。我正在运行 Linux Mint 并且到目前为止使用 python manage.py runserver 命令没有遇到任何问题。

好吧,今天我启动了应用程序并启动了本地服务器,没有任何问题。然后我尝试在浏览器中打开该应用程序并收到相当可怕的错误消息:

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

自从我上次使用该应用程序以来,我还没有接触过它,并且当时它运行良好。我真的不知道这是在说什么,因为我没有实现缓存,也没有使用数据库……我问过一些熟悉 Django 的大学,也不知道这是什么。有任何想法吗?我还尝试根据在线帖子的建议将 DATABASES 设置为空 dict ,但没有效果。有点亏。

编辑:我想我应该提一下,自从我上次接触这个项目以来,我已经开始并建立了一个使用数据库的新 Django 项目。这个项目是否可能以某种方式被缓存并破坏了我目前正在尝试的项目?

EDIT2:我应该指出,这个 web 应用程序目前在 http://mrcagney-maps.com 运行和工作。代码完全一样(自从我上次推送到服务器后就没有碰过它)。真的很奇怪。

【问题讨论】:

【参考方案1】:

对于未来的 Google 员工 - 我遇到了这个问题,上述解决方案对我不起作用。 对我有用的是在 Chrome 中为 127.0.0.1 URL 清除/删除我的 cookie。因此,转到设置或按CMD+,,然后按 Cookie 和其他站点数据,然后找到 127.0.0.1 或 localhost 并删除这些 cookie。刷新本地开发主机页面,错误应该消失了。这与损坏的会话/cookie 文件有关。

【讨论】:

真正的救生员——这可能救了我几个小时以上。希望我能再次投票。 这会影响产品吗?或者这是 chrome localhost 的事情? @MichaelDavidson 这是一个 chrome localhost 的东西。删除损坏的 cookie 不会影响生产。 omg 非常感谢您,尝试了很多会话,例如添加和删除我最近安装的应用程序,但仍然出现错误,谢谢。【参考方案2】:

错误AttributeError: 'SessionStore' object has no attribute '_session_cache' 可能源于数据库没有 django_session 表。但是,由于您没有使用表格,因此您需要确保项目设置文件中的 MIDDLEWARE_CLASSES 中没有 'django.contrib.sessions.middleware.SessionMiddleware'。如果它在那里,它会寻找一个存储会话的数据库表,导致上述错误。

【讨论】:

嘿,谢谢。我会在星期一回到我的机器时尝试一下,但我感觉我试过了,但收到了一个关于缺少中间件的不同错误(即使我没有使用会话)。奇怪的是,这个应用程序目前在实时服务器上运行良好,同时注释掉了 DATABASES 设置并且包含了中间件类。真奇怪。我不明白它是如何在网上工作的......但它一直是现在...... 更新:好的,所以你对中间件是正确的。为了让它再次工作,我必须删除以下内容:'django.contrib.sessions.middleware.SessionMiddleware''django.contrib.auth.middleware.AuthenticationMiddleware''django.contrib.auth.middleware.SessionAuthenticationMiddleware''django.contrib .messages.middleware.MessageMiddleware' 说实话,不知道他们为什么在那里。可能是我之前的项目遗留下来的 我仍然不明白为什么包含它们并没有破坏实时服务器上的版本,而是在我的本地计算机上破坏了版本。我认为这与 Django 的 python manage.py runserver 命令在项目之间缓存某些内容有关。我之前从事的项目有用户身份验证和会话等,我相信在本地运行该应用程序的一些残余破坏了我的其他应用程序。类似于在本地缓存用户,并且在切换应用程序时仍会寻找该用户/会话。这就是我能想到的。反正。它现在正在工作,所以我接受了你的回答。干杯。 我最好的猜测是您可能已经在生产中覆盖了中间件。只是一个猜测。它也应该在那里爆炸。很高兴知道我帮忙了!【参考方案3】:

我不确定为什么我开始收到此错误,但它涉及升级。我刚刚删除了所有会话,重新登录后一切正常。

# from the shell but equivalent sql would work fine too
from django.contrib.sessions.models import Session
Session.objects.all().delete()

【讨论】:

这解决了我的问题。我更改了 django 的密钥,从而使当前会话无效。【参考方案4】:

这对我有用。由于您的应用程序中没有数据库。默认情况下,管理页面会查找数据库。所以首先让我们创建默认数据库。

关闭服务器并运行

python manage.py makemigrations
python manage.py migrate

现在为您的应用程序创建管理员或超级用户。填写用户名和密码。

python manage.py createsuperuser

现在重启你的服务器并进入管理页面

python manage.py runserver

【讨论】:

我重命名了项目,答案对我有用。 非常感谢。我遇到了同样的问题,它对我有用【参考方案5】:

升级我的 wagtail 版本后对我抛出了同样的错误......所以,对我来说,解决方案更简单,或者你没有注意到:D 实际上我打开了浏览器 > F12 > 存储选项卡并删除所有 Cookie 和所有其他缓存数据

【讨论】:

解决问题的好方法。删除站点的会话 cookie 就足够了,可以使用浏览器开发工具或 EditThisCookie 等方便的插件来完成。【参考方案6】:

通过 Postman 遇到此问题的用户请确保 Cache-Control 的请求标头中的值是 no-cache

如果不是,则清除域的 cookie 数据。

【讨论】:

【参考方案7】:

正常情况下我们使用

from django.http import JsonResponse

为了发送响应,我们不需要使用会话。

但是当我们使用一些包时,例如djangorestframework,我们强制使用会话。

会话需要存储在 sqllite,mysql ,...或其他中 那么我们需要运行这些命令:

python manage.py makemigrations
python manage.py migrate

【讨论】:

以上是关于寻找“属性'_session_cache'”的Django项目的主要内容,如果未能解决你的问题,请参考以下文章

歌曲怎么改成DJ版

谁知道2009最新的DJ舞曲啊

DJ软件 XYLIO Future DJ Pro v1.5.2.0 MacOSX

R:使用“传播”功能进行旋转

ImportError:导入 dj_database_url ImportError:没有名为“dj_database_url”的模块

坦克大战DJ。。。我想把这首歌放到QQ空间