【问题标题】:TemplateDoesNotExist, but file foundTemplateDoesNotExist,但找到文件
【发布时间】:2015-09-06 23:42:30
【问题描述】:

我在我的 Django 项目中收到一个奇怪的 TemplateDoesNotExist 错误。加载程序实际上找到了一个模板文件(它也存在并且我想调用!)。这是输出:

Template-loader postmortem

Django tried loading these templates, in this order:

Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
    [...]/anaconda3/lib/python3.4/site-packages/django/contrib/admin/templates/osm/index.html (File does not exist)
    [...]/anaconda3/lib/python3.4/site-packages/django/contrib/auth/templates/osm/index.html (File does not exist)
    [...]/projekt/osm/templates/osm/index.html (File exists)
    [...]/projekt/flotte/templates/osm/index.html (File does not exist)

为什么找到了文件还是报错?

编辑:经过一些测试,似乎所有扩展我的base.html 的 HTML 模板都不起作用,而独立的 html 模板工作:

{% extends 'base.html' %}
{% load static %}
{% block main_content %}
[...]

我的base.html 位于[...]/projekt/static/templates。此路径在 TEMPLATE_DIRS 中定义。很确定这在更新到 django 1.8.2 之前有效! :(

如何制作项目模板路径[...]/projekt/static/templates

我的设置:[完整见http://pastebin.com/rn2hSj5Y]

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)
#Template location
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, "static", "templates"),
    #os.path.join(PROJECT_PATH, "osm", "templates", "osm"),
    #os.path.join(PROJECT_PATH, "flotte", "templates", "flotte"),
)

我的对应观点:

def index(request):
    # Request the context of the request.
    # The context contains information such as the client's machine details, for example.
    context = RequestContext(request)

    context_dict = {'boldmessage': "I am bold font from the context"}

    return render_to_response('osm/index.html', context_dict, context)

【问题讨论】:

  • 你检查文件的权限了吗?
  • 是的,[...]/projekt/osm/templates/osm/index.html 可读:-rwxrwxr-- 1 user plugdev 982 Oct 5 2014 index.html*
  • 我猜你没有使用 Django 1.8 - 你用的是什么版本? 1.8 更改模板设置。
  • 我使用的是 1.8.2,但我相信设置文件以及应用程序“OSM”是为 1.7 编写的。请查看我的编辑,了解哪些 HTML 失败!

标签: python django


【解决方案1】:

如果 osm , fllotte 是应用程序,只需从 TEMPLATE_DIRS 中删除这两行:

 os.path.join(PROJECT_PATH, "osm", "templates", "osm"),
 os.path.join(PROJECT_PATH, "flotte", "templates", "flotte"),

因为它们已经被应用目录加载器找到了。 TEMPLATE_DIRS 用于非应用模板。

【讨论】:

  • 嘿amr,谢谢你的回答。我试过这个,但我找不到它有什么不同。在更改 settings.py 以部署更改后,除了重新启动服务器之外,我是否需要做任何其他事情?
猜你喜欢
  • 2020-09-08
  • 2017-02-01
  • 1970-01-01
  • 2015-06-13
  • 2020-07-14
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多