我正在尝试在我的缓存中相同的一些查询,目前我正在使用我在这里找到的示例,但是当我尝试打开模板时出现此错误。
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')}
正如错误所说,上下文处理器应该提供一个参数,而你没有。您的另一个问题是您返回的是集合,而不是字典。所有上下文处理器都应返回字典 {key: value, ...},其中 key 将是模板上下文中变量的名称,并值变量的值
def cached_queries(request):
return {'cache': cache.get('cie_1')}
除了接受参数外,上下文处理器还需要返回字典,而不是集合。
def cached_queries(request):
return {'cache': cache.get('cie_1')}