在django中启用app模板的覆盖?

时间:2010-01-23 19:27:46

标签: python django templates

我正在编写一个简单的网站来显示某些数据的统计数据,我有一个名为“stats”的应用程序,我想为(stats / templates / stats中的位置)编写默认模板,但是我希望这些可以像管理员应用程序的模板一样覆盖。 IE:如果我在我的项目的模板目录中放置了stats / view.html,它将被用于而不是我的apps目录中的那个。我似乎无法弄清楚如何做到这一点。如何让Django在点击应用程序之前搜索项目的TEMPLATE_DIRS?

编辑:发现问题,看到某人使用os模块设置模板目录的教程。他们有:

TEMPLATE_DIRS = (
    os.path.join(os.path.basename(__file__), 'templates')
)

应该是:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates')
)

1 个答案:

答案 0 :(得分:2)

您需要在filesystem设置中将app_directories加载程序加载到 TEMPLATE_LOADERS加载程序之前。

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

TEMPLATE_LOADERS问题的顺序。