I'm trying to same some query in my cache, currently I'm working with an example that I found here, but I get this error, when I try to open my template.

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)

Here's my code.

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')}

2 Answers

1
Daniel Roseman On Best Solutions

As well as taking an argument, context processors need to return a dictionary, not a set.

def cached_queries(request):
    return {'cache': cache.get('cie_1')}
3
Tom Carrick On

Just as the error says, context processors should provide an argument, and you aren't. Your other issue is that you're returning a set, not a dictionary. All context processors should return a dictionary {key: value, ...}, where key will be the name of the variable in your template's context, and value the value of the variable

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