【问题标题】:Get all templates django detects from TEMPLATE_LOADERS and TEMPLATE_DIRS获取 django 从 TEMPLATE_LOADERS 和 TEMPLATE_DIRS 检测到的所有模板
【发布时间】:2013-06-11 07:11:40
【问题描述】:

TEMPLATE_DIRS = ('/path/to/templates/',)

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

我正在尝试找到一种解决方案,该解决方案将列出我在这些位置(TEMPLATE_DIRSTEMPLATE_LOADERS)中的指定目录的内容。

我需要类似的东西:

template_files = []
for dir in EVERY_DIRECTORY_DJANGO_LOOKS_FOR_TEMPLATES_IN:
    template_files.append(os.listdir(dir))

【问题讨论】:

  • settings.TEMPLATE_DIRS + settings.TEMPLATE_LOADERS 不行吗?
  • 我可以遍历 settings.TEMPLATE_DIRS,但是 settings.TEMPLATE_LOADERS 只是一个模块的元组,而不是 dirs...

标签: django python-2.7 django-templates


【解决方案1】:

由于模板可以位于基本模板位置下的嵌套目录中,我建议使用os.walk 来获取您需要的模板,它本质上是os.listdir 的包装器,将跟随目录。

django.template.loaders.app_directories.app_template_dirs 是所有应用模板目录的内部元组,TEMPLATE_DIRSdjango.template.loaders.filesystem.Loader 使用的设置。

以下代码应生成模板目录中所有可用文件的列表(这可能包括非模板文件):

from django.conf import settings
from django.template.loaders.app_directories import app_template_dirs

import os

template_files = []
for template_dir in (settings.TEMPLATE_DIRS + app_template_dirs):
    for dir, dirnames, filenames in os.walk(template_dir):
        for filename in filenames:
            template_files.append(os.path.join(dir, filename))

【讨论】:

    【解决方案2】:

    如果有人仍然需要这个,我正在运行 1.9.2,它看起来像 app_template_dirs 现在是 get_app_template_dirssettings.TEMPLATE_DIRS 现在是 settings.TEMPLATES[0]['DIRS']

    这就是我所做的:

    from django.conf import settings
    from django.template.loaders.app_directories import get_app_template_dirs
    import os
    
    template_dir_list = []
    for template_dir in get_app_template_dirs('templates'):
        if settings.ROOT_DIR in template_dir:
            template_dir_list.append(template_dir)
    
    
    template_list = []
    for template_dir in (template_dir_list + settings.TEMPLATES[0]['DIRS']):
        for base_dir, dirnames, filenames in os.walk(template_dir):
            for filename in filenames:
                template_list.append(os.path.join(base_dir, filename))
    

    然后您可以根据需要使用 template_list 遍历列表:

    for template in template_list:
        print template
    

    【讨论】:

    • 请注意,虽然这可能很有用,但template_list 是一个路径名列表,用于标识 DjangoTemplates 后端使用的文件,假设它是 TEMPLATES 设置中的第一个。其他模板可以通过其他后端访问,它们可能会也可能不会使用其他文件。 Django 中无法使用模板后端(旧 django 版本的“加载器”)列出所有可能的模板名称,因为模板后端协议中没有定义列出名称。
    【解决方案3】:

    使用 Django 的模板加载器返回所有路径的列表,然后使用 pathlib 到 blob(递归)每个目录以查找所有 .html 文件:

    from pathlib import Path
    
    from django import template as django_template
    
    
    def get_all_templates_files():
        dirs = []
        for engine in django_template.loader.engines.all():
            # Exclude pip installed site package template dirs
            dirs.extend(x for x in engine.template_dirs if 'site-packages' not in str(x))
        files = []
        for dir in dirs:
            files.extend(x for x in Path(dir).glob('**/*.html') if x)
        return files
    

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 2015-06-30
      • 1970-01-01
      • 2021-12-16
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      相关资源
      最近更新 更多