【问题标题】:Django can't find template dir?Django 找不到模板目录?
【发布时间】:2012-07-18 16:40:24
【问题描述】:

最初我的 Django 项目中只有一个应用程序,模板由 index.html、detail.html 和 layout.html 组成……索引和详细信息文件扩展了布局。他们都住在同一个目录中([app1]/templates/[app1]/),它可以很好地找到所有文件。

现在我有了第二个应用程序,我想重新使用 layout.html ... 我决定在 django 项目的基础上创建一个模板目录,并将其放入其中。这是我的目录结构现在的样子:

<basepath>/<django_project>/templates/shared
<basepath>/<django_project>/<app1>/templates/<app1>
<basepath>/<django_project>/<app2>/templates/<app2>

我更新了我的 settings.py:

TEMPLATE_DIRS = (
    '/full/path/to/<django_project>/<app1>/templates',
    '/full/path/to/<django_project>/<app2>/templates',
    '/full/path/to/<django_project>/templates',
)

在“共享”目录中,我有来自 app1 或 app2 模板目录的 layout.html ...,我有 index.html 和文件顶部的“扩展”行:

{% extends "shared/layout.html" %}

但是,当我尝试加载应用程序视图时,它会收到一个错误,即找不到 shared/layout.html

TemplateSyntaxError at /
Caught TemplateDoesNotExist while rendering: shared/layout.html

任何想法我错过了什么?这应该很简单,我一定忽略了一些非常明显的东西?

【问题讨论】:

  • 是否可以显示您的 TEMPLATE_LOADERS 设置?
  • 另请注意,如果您在设置的TEMPLATE_LOADERS(这是默认设置)中启用了django.template.loaders.app_directories.Loader,则无需在其中添加提及“每个应用程序”模板目录TEMPLATE_DIRS.
  • 从错误页面中,“Django 尝试按此顺序加载这些模板:”部分说什么?
  • 注意!永远不要将您的应用程序目录放在您的 TEMPLATE_DIRS 设置中。这就是 TEMPLATE_LOADERS 的用途。

标签: python django django-templates


【解决方案1】:

我的错!我以为我在 Nginx 后面运行它,所以我重新启动它,但仍然遇到问题。重新检查后,我意识到我在 Apache 后面运行,重新启动 Apache 更新了我的 TEMPLATE_DIRS,现在它可以工作了!

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 2012-03-20
    • 2018-01-02
    • 2012-07-10
    • 2015-03-14
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多