我们所有的模板 dirs 都是一个模板 dir 的子文件夹, 所以我们只需要让装载器先查看那里。
但它并没有改变AFAICT的行为。
我们之所以这样做,是因为我们扩展了基础装载器,使用了这样的模块 option_path.py
:
class Loader(filesystem.Loader):
"""
Looks for templates in an alternative path.
Allows a template to extend a template which is located in a specified views path.
"""
is_usable = True
__view_paths = None
def __init__(self, views_path):
if Loader.__view_paths is None:
Loader.__view_paths = [views_path]
def get_alternative_template_sources(self, template_name):
for template_dir in Loader.__view_paths:
try:
yield safe_join(template_dir, template_name)
except UnicodeDecodeError:
# The template dir name was a bytestring that wasn t valid UTF-8.
raise
except ValueError:
# The joined path was located outside of this particular
# template_dir (it might be inside another one, so this isn t
# fatal).
pass
def get_template_sources(self, template_name, template_dirs):
return itertools.chain(
filesystem.Loader.get_template_sources(self, template_name, template_dirs),
Loader.get_alternative_template_sources(self, template_name))
然后在同一模块中:
= 装载器
然后在 main. py
中:
def main():
views_path = os.path.join(os.path.dirname(__file__), templates )
settings.TEMPLATE_LOADERS = (( web.rich.templates.alternative_path.Loader , views_path),
django.template.loaders.filesystem.Loader ,
django.template.loaders.app_directories.Loader )