2 Wrapper for loading templates from the filesystem.
5 from django
.conf
import settings
6 from django
.template
import TemplateDoesNotExist
7 from django
.utils
._os
import safe_join
9 def get_template_sources(template_name
, template_dirs
=None):
11 Returns the absolute paths to "template_name", when appended to each
12 directory in "template_dirs". Any paths that don't lie inside one of the
13 template dirs are excluded from the result set, for security reasons.
16 template_dirs
= settings
.TEMPLATE_DIRS
17 for template_dir
in template_dirs
:
19 yield safe_join(template_dir
, template_name
)
20 except UnicodeDecodeError:
21 # The template dir name was a bytestring that wasn't valid UTF-8.
24 # The joined path was located outside of this particular
25 # template_dir (it might be inside another one, so this isn't
29 def load_template_source(template_name
, template_dirs
=None):
31 for filepath
in get_template_sources(template_name
, template_dirs
):
33 return (open(filepath
).read().decode(settings
.FILE_CHARSET
), filepath
)
35 tried
.append(filepath
)
37 error_msg
= "Tried %s" % tried
39 error_msg
= "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
40 raise TemplateDoesNotExist
, error_msg
41 load_template_source
.is_usable
= True