【问题标题】:Django cache not working cached_queries() takes no arguments (1 given)Django 缓存不起作用 cached_queries() 不接受任何参数(给定 1 个)
【发布时间】:2016-05-20 02:54:04
【问题描述】:

我正在尝试在我的缓存中进行相同的查询,目前我正在使用我在此处找到的示例,但是当我尝试打开我的模板时出现此错误。

cached_queries() takes no arguments (1 given)



Internal Server Error: /consulta-inicial/
Traceback (most recent call last):
File "/home/gjce/.virtualenvs/medi1.8/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 164, in get_response
response = response.render()
File "/home/gjce/.virtualenvs/medi1.8/local/lib/python2.7/site-packages/django/template/response.py", line 158, in render
self.content = self.rendered_content
File "/home/gjce/.virtualenvs/medi1.8/local/lib/python2.7/site-packages/django/template/response.py", line 135, in rendered_content
content = template.render(context, self._request)
File "/home/gjce/.virtualenvs/medi1.8/local/lib/python2.7/site-packages/django/template/backends/django.py", line 74, in render
return self.template.render(context)
File "/home/gjce/.virtualenvs/medi1.8/local/lib/python2.7/site-packages/django/template/base.py", line 208, in render
with context.bind_template(self):
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/home/gjce/.virtualenvs/medi1.8/local/lib/python2.7/site-packages/django/template/context.py", line 241, in bind_template
updates.update(processor(self.request))
TypeError: cached_queries() takes no arguments (1 given)

这是我的代码。

form.py

cie_4 = DropdownCie(cie_descripcion.objects.all().order_by('cie_descripcion_desc').order_by('cie_descripcion_desc'), required=False)
cache.set('cie1', cie_1)

settings.py

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [PROJECT_DIR.child("templates")],
    'APP_DIRS': False,
    'OPTIONS': {
        'context_processors': [
            "expmedico.context_processors.cached_queries",
        ],
    },
},]

context_processors.py

from django.core.cache import cache

def cached_queries():
    return {'cache', cache.get('cie_1')}

【问题讨论】:

  • 您能否编辑您的问题以包含完整的回溯?

标签: python django templates caching


【解决方案1】:

正如错误所说,上下文处理器应该提供一个参数,而你没有。您的另一个问题是您返回的是集合,而不是字典。所有上下文处理器都应该返回一个字典 {key: value, ...},其中 key 是模板上下文中变量的名称,value 是变量的值

def cached_queries(request):
    return {'cache': cache.get('cie_1')}

【讨论】:

  • 是的,但如果我添加任何参数,如请求或自我,我会得到其他错误。无法将字典更新序列元素 #0 转换为序列
  • @MacAlexander 这将是另一个错误(也是您应该发布的错误),因为文档明确指出上下文处理器应该“将请求对象作为其参数”。 cache.get 返回的究竟是什么?
  • 一个查询集... cie_1 =cie_descripcion.objects.all().order_by('cie_descripcion_desc')
  • 我已经编辑了我的答案以显示您的第二个问题,但我应该感谢丹尼尔,他的答案比我先到。
【解决方案2】:

除了接受一个参数,上下文处理器还需要返回一个字典,而不是一个集合。

def cached_queries(request):
    return {'cache': cache.get('cie_1')}

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 2023-03-07
    • 2011-09-30
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多