我的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)
答案 0 :(得分:0)
如果osm,flotte是应用程序,只需从TEMPLATE_DIRS中删除这两行:
os.path.join(PROJECT_PATH, "osm", "templates", "osm"),
os.path.join(PROJECT_PATH, "flotte", "templates", "flotte"),
因为应用程序目录加载器已经找到它们。 TEMPLATE_DIRS适用于非应用模板。