如何获取Django 2.0将在其中查找模板的所有目录?

时间:2018-07-09 10:43:16

标签: django python-3.x django-templates

我想检索Django将在其中搜索模板文件的所有可能目录的列表。我想列出这些目录的可重复列表

我尝试了以下方法:

def get_template_directories():
    template_directories = []
    for template in settings.TEMPLATES:
        for directory in template['DIRS']:
            template_directories.append(directory)
return template_directories

但是,我想要Django查找模板的所有目录,包括INSTALLED_APPS中的模板

1 个答案:

答案 0 :(得分:0)

我在Django中搜索,找不到任何有用的API来获取Django中包含模板的所有目录。我最终决定像下面这样编写我的代码:

def get_template_directories():
    template_directories = []

    # Getting templates in the templates directory
    for template in settings.TEMPLATES:
        for directory in template['DIRS']:
            template_directories.append(directory)

    # Getting templates from INSTALLED_APPS
    for app in  settings.INSTALLED_APPS:
        path = join(apps.get_app_config(app.split('.')[-1]).path, 'templates')
        if exists(path):
            template_directories.append(path)

    return template_directories