TemplateDoesNotExist error when postmortem says it exists?

548 views
Skip to first unread message

Rob Hudson

unread,
Mar 29, 2009, 11:18:09 AM3/29/09
to Django users
This is a stumper...

I have a very simple view that is a wrapper around the generic view
direct_to_template. The loader finds the template as indicated in the
output "(File exists)", but yet I still get a TemplateDoesNotExist
error. Any ideas?

I can go to the Django shell and load it just fine:
> ./manage.py shell
>>> from django.template.loader import get_template
>>> t = get_template('book/search_form.html')
>>> t
<django.template.Template object at 0x1505d90>
>>> t.name
'book/search_form.html'

Copy/Paste from debug output:

Template Loader Error:
Django tried loading these templates, in this order:
Using loader
django.template.loaders.filesystem.load_template_source:
/Users/rob/git/anglers/anglers/templates/book/search_form.html
(File exists)
Using loader
django.template.loaders.app_directories.load_template_source:
/Users/rob/django/django/django/contrib/admin/templates/book/
search_form.html (File does not exist)

Traceback:
File "/Users/rob/django/django/django/core/handlers/base.py" in
get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/Users/rob/django/django/django/contrib/auth/decorators.py" in
__call__
67. return self.view_func(request, *args, **kwargs)
File "/Users/rob/git/anglers/anglers/../anglers/book/views.py" in
search_form
102. extra_context=extra_context,
File "/Users/rob/django/django/django/views/generic/simple.py" in
direct_to_template
17. t = loader.get_template(template)
File "/Users/rob/django/django/django/template/loader.py" in
get_template
81. source, origin = find_template_source(template_name)
File "/Users/rob/django/django/django/template/loader.py" in
find_template_source
74. raise TemplateDoesNotExist, name

Exception Type: TemplateDoesNotExist at /advanced-search/
Exception Value: book/search_form.html

I'm using Django 1.1 beta 1.

Thanks,
Rob

Karen Tracey

unread,
Mar 29, 2009, 11:28:33 AM3/29/09
to django...@googlegroups.com
On Sun, Mar 29, 2009 at 11:18 AM, Rob Hudson <trebor...@gmail.com> wrote:

This is a stumper...

I have a very simple view that is a wrapper around the generic view
direct_to_template.  The loader finds the template as indicated in the
output "(File exists)", but yet I still get a TemplateDoesNotExist
error.  Any ideas?

That generally means the permissions don't allow the code to access the file.  You don't mention if this happens with the dev server (which would surprise me, since you can load the template from the shell) or only with a real web server. If it's only with the real web server than I expect the permissions need to be fixed so that the web server can read the template file.

Karen

Rob Hudson

unread,
Mar 29, 2009, 1:26:29 PM3/29/09
to django...@googlegroups.com
On Sun, Mar 29, 2009 at 8:28 AM, Karen Tracey <kmtr...@gmail.com> wrote:
> That generally means the permissions don't allow the code to access the
> file.  You don't mention if this happens with the dev server (which would
> surprise me, since you can load the template from the shell) or only with a
> real web server. If it's only with the real web server than I expect the
> permissions need to be fixed so that the web server can read the template
> file.

Yes, it's using the dev server. I did check the permissions just in
case and they look fine (-rw-r--r--).

What is odd is I'm also using this template from a different view and
it loads fine. I'm trying to debug now but I'm about to run out of
time and thought I'd throw it out here if anyone else encountered
this.

Thanks,
-Rob

Rob Hudson

unread,
Mar 29, 2009, 1:42:15 PM3/29/09
to Django users
Wow, that was a tricky one to track down...

After putting debug output in django/template/loaders/filesystem.py I
saw that filepath was set to:

'/Users/rob/git/anglers/anglers/templates/('book/
search_form.html',)'

I back tracked that and found that I had a trailing comma in my view
code:

if not template:
template = 'book/search_form.html',

I take it that gets interpreted as a single value tuple. D'oh! That
got there from a refactor where I copy/pasted that string when it was
in an extra_context dictionary and didn't notice the comma.

What's interesting to me, is that the file path got converted
correctly in the debug output but not in the template loader itself.

-Rob

Russell Keith-Magee

unread,
Mar 29, 2009, 7:30:06 PM3/29/09
to django...@googlegroups.com
On Mon, Mar 30, 2009 at 1:42 AM, Rob Hudson <trebor...@gmail.com> wrote:
>
> Wow, that was a tricky one to track down...
...

> I take it that gets interpreted as a single value tuple.  D'oh!  That

It does, operator precedence notwithstanding (for example, a lambda
expression returning a tuple that is being used as an argument to a
function may not interpret an unbracketed tuple, but as a second
function argument)

> What's interesting to me, is that the file path got converted
> correctly in the debug output but not in the template loader itself.

That's an edge case of string variable expansion:

print "Hello %s world" % foo

and:

print "Hello %s world" % (foo,)

give the same result. However, this flexibility isn't automatically
extended to any other usage of a single value tuple - the code needs
to explicitly allow tuples for input. The template loader doesn't.

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages