【发布时间】: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 失败!