在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ Django 查找 templates 時設(shè)置 'DIRS' 路徑的問題?

Django 查找 templates 時設(shè)置 'DIRS' 路徑的問題?

我的主項目目錄下,和 APP 目錄下都有 templates 文件夾,為了讓 Django 能找到模板,我看很多資料,包括官方 tutorial 里面都是說要把 settings 設(shè)置里面,TEMPLATES 的 'DIRS' 設(shè)置如下:

TEMPLATES = [{
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
}]

這樣就明確了去項目根目錄下的 templates 文件夾中找。

但是我發(fā)現(xiàn),路徑只填一個'templates'也可以:

TEMPLATES = [{
    'DIRS': ['templates'],
}]

效果一樣,也能成功在項目根目錄下的 templates 文件夾中找到模板文件。

所以我的問題是,為什么只填一個'templates'也行呢?也能代表根目錄下的 templates 呢?另外為什么所有資料都沒提到能這樣簡寫呢,這樣寫是會有什么坑嗎?

新手初學(xué),多謝指教!

回答
編輯回答
初念

源碼里是這么實現(xiàn)位置查找的

class EngineMixin:
    def get_template(self, template_name):
        return self.engine.get_template(template_name)

    @cached_property
    def engine(self):
        return self.backend({
            'APP_DIRS': True,
            'DIRS': [os.path.join(ROOT, self.backend.app_dirname)],
            'NAME': 'djangoforms',
            'OPTIONS': {},
        })


[docs]class DjangoTemplates(EngineMixin, BaseRenderer):
    """
    Load Django templates from the built-in widget templates in
    django/forms/templates and from apps' 'templates' directory.
    """

    backend = DjangoTemplates


import django
django.__path__[0] + '/forms/templates'
2017年9月28日 19:50