Django缓存设置阻止所有命令

时间:2015-09-15 08:56:30

标签: python django caching

我的问题是,我为django配置了一个数据库缓存。现在,如果我清除数据库,则没有命令正在运行:

Traceback (most recent call last):
  File "app/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/cms/admin/__init__.py", line 11, in <module>
    plugin_pool.plugin_pool.discover_plugins()
  File "/usr/local/lib/python2.7/site-packages/cms/plugin_pool.py", line 35, in discover_plugins
    invalidate_cms_page_cache()
  File "/usr/local/lib/python2.7/site-packages/cms/views.py", line 340, in invalidate_cms_page_cache
    version = _get_cache_version()
  File "/usr/local/lib/python2.7/site-packages/cms/views.py", line 285, in _get_cache_version
    version = cache.get(CMS_PAGE_CACHE_VERSION_KEY)
  File "/usr/local/lib/python2.7/site-packages/django/core/cache/backends/db.py", line 65, in get
    "WHERE cache_key = %%s" % table, [key])
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "cache_table" does not exist
LINE 1: SELECT cache_key, value, expires FROM "cache_table" WHERE ca...

很明显,cache_table丢失了。但由于此错误,我无法使用manage.py createcachetable生成新的..

这是缓存配置:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'cache_table',
    }
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

查看回溯,似乎应用程序在创建之前尝试访问缓存表。您可以尝试暂时禁用CMS应用程序,直到创建缓存表为止。

如果你不能这样做,创建一个新的Django项目,使用相同的数据库和缓存设置,并使用新项目中的./manage.py createcachetable命令创建表。

答案 1 :(得分:1)

我遇到了相同的鸡蛋和鸡蛋情况,似乎如果你设置CACHES设置(甚至是空字典),Django CMS会在你运行python manage.py ...时尝试访问它你无法创建缓存表。

但是createcachetable有一个方便的--settings选项,我最后在我的实际settings_nocache.py旁边创建了一个settings.py,其中包含以下内容:

from .settings import *
del CACHES

并运行以下命令来创建我的缓存表:

python manage.py createcachetable --settings settings_nocache default_cache

--settings选项后面的参数是python import 路径,而不是 filesystem 路径。