cached template loader

255 views
Skip to first unread message

Sævar Öfjörð

unread,
Jul 6, 2010, 8:41:38 AM7/6/10
to Django users
Hi, I've been using the default template loaders and it works fine.
Now I want to add the cached template loader, but I get an error.

I'm using a wrapper function to return responses that include the
request in the context, like this:

# file helpers/helpers.py
from django.template import RequestContext, loader
from django.http import HttpResponse

def return_response(request, dictionary, template, headers={}):
t = loader.get_template(template)
c = RequestContext(request, dictionary)
res = HttpResponse(t.render(c))
for h, v in headers.iteritems():
res[h] = v
return res

The stacktrace:

Traceback:
File "/home/django/project/virtualenv/lib/python2.6/site-packages/
django/core/handlers/base.py" in get_response
106. response = middleware_method(request,
e)
File "/home/django/project/virtualenv/lib/python2.6/site-packages/
django/core/handlers/base.py" in get_response
100. response = callback(request,
*callback_args, **callback_kwargs)
File "/home/django/project/project/views.py" in index
24. return helpers.return_response(request, d, 'frontpage.html')
File "/home/django/project/project/helpers/helpers.py" in
return_response
129. t = loader.get_template(template)
File "/home/django/project/virtualenv/lib/python2.6/site-packages/
django/template/loader.py" in get_template
157. template, origin = find_template(template_name)
File "/home/django/project/virtualenv/lib/python2.6/site-packages/
django/template/loader.py" in find_template
128. loader = find_template_loader(loader_name)
File "/home/django/project/virtualenv/lib/python2.6/site-packages/
django/template/loader.py" in find_template_loader
104. func = TemplateLoader(*args)

Exception Type: TypeError at /
Exception Value: __init__() takes exactly 2 arguments (1 given)

Is there some different way I should approach the template loading?

Sævar Öfjörð

unread,
Jul 6, 2010, 8:45:27 AM7/6/10
to Django users
I've also tried passing this through
django.shortcuts.render_to_response, but I get the same error.
- Sævar

Chris Cogdon

unread,
Dec 18, 2012, 3:08:58 PM12/18/12
to django...@googlegroups.com
your two versions are actually identical :)

The missing comma, I assume, was missing after django.template.loaders.app_directories.Loader ... without the comma there, python sees two strings right next to each other, and thus concatenates them, resulting in a single parameter : 'django.template.loaders.app_directories.Loaderdjango.template.loaders.filesystem.Loader'

On Tuesday, December 18, 2012 2:40:02 AM UTC-8, Tom Martin wrote:
I had the same problem and discovered that I was missing a comma in the settings file:

TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader, (
        'django.template.loaders.app_directories.Loader',
        'django.template.loaders.filesystem.Loader,
    ))
)

should have been:

TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader, (
        'django.template.loaders.app_directories.Loader',
        'django.template.loaders.filesystem.Loader,
    ))
)

Pretty subtle issue, I'm not sure how the missing comma causes it.
Reply all
Reply to author
Forward
0 new messages