Django模板查找顺序 [英] Django templates lookup order

查看:124
本文介绍了Django模板查找顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用django-floppyforms的应用程序.另外,我的应用程序提供了默认的twitter bootstrap外观,因此我在我的应用程序中自定义了软盘模板,以使其自举.我将floppyforms/{layouts, rows}/bootstrap放入应用程序的模板目录中.但这是行不通的:django不会使用它们.因此,我不想强​​制最终用户将自定义模板放入他的项目中,实际上,我想指出django在呈现应用内内容时要使用我的本地floppyforms模板.我只想让我的应用程序独立运行,没有任何不便之处.

I'm writing an app, that uses django-floppyforms. Also, my app provides the default twitter bootstrap skin, so i have customized floppyforms templates im my app to make them bootstrap alike. I put floppyforms/{layouts, rows}/bootstrap into my app's templates directory. But it does not work: django won't use them. So, i don't want to enforce end user to put customized templates into his project, in fact, i want to point django to take my local floppyforms templates when it renders in-app content. I just want to make my app standalone without any unhandy depencies.

现在,我在 django-admintools-bootstrap Django 1.5.1.它是在INSTALLED_APPS中的admin_tools之前添加的,但是没有效果.同样,它不会为django-admintools-bootstrap收集静态信息.在使用这两个软件包和Django 1.4的其他类似项目中,一切都很好.另外,我检查了Django 1.5的发行说明中模板查找顺序的更改,但未发现任何更改.

Now i'm having the similar troubles with django-admintools-bootstrap and Django 1.5.1. It was added before admin_tools in INSTALLED_APPS, but there's no effect. Also it won't collect static for django-admintools-bootstrap. In other similar projects using this two packages and Django 1.4, all works fine. Also, i've checked the release notes for Django 1.5 for template lookup order changes and found nothing about it.

推荐答案

更新后的答案:

从Django 1.8开始, TEMPLATE_DIRS 和已弃用 TEMPLATE_LOADERS 并由 TEMPLATES .

As of Django 1.8, TEMPLATE_DIRS and TEMPLATE_LOADERS are deprecated and replaced by TEMPLATES.

TEMPLATES 的示例可以是:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            # 'loaders': [
            #     'django.template.loaders.filesystem.Loader',
            #     'django.template.loaders.app_directories.Loader',
            # ],
            'debug': True,
        },
    },
]

模板查找顺序由以下各项决定:

The template lookup order is by dictated by the following:

  • 最重要的地方是 < OPTIONS .如果已定义,则要求 APP_DIRS 没有设置并在那里遵循任何明确的顺序.

  • Essentialy the most important place is the loaders option of the OPTIONS. If this is defined, it requires that APP_DIRS is not set and follows any explicit order there.

如果没有装载程序,并且有 DIRS 定义的,它们优先于filesystem加载程序.

If no loaders, and if any DIRS defined, these have priority as the filesystem loader.

如果没有装载程序,请 APP_DIRS ,它们的优先级高于 DIRS .

If no loaders, and APP_DIRS is defined, these have second priority over DIRS.

以上内容并未明确记录,但可以很容易地通过现有文档进行推断,也可能经过一些实验.

The above are not explicitly documented, but can easily be deducted by the existing documentation and maybe after some experiments.

这篇关于Django模板查找顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆