【发布时间】:2015-11-21 09:41:23
【问题描述】:
我收到以下错误:
TemplateDoesNotExist at /app1/1/about/
index/index.html
但是模板加载器事后说:
/var/www/web/sites/mysite.com/app1/templates/index/index.html(文件存在)
我已经尝试了 stackoverflow 的所有类似问题的答案,但它们对我不起作用。在我的本地服务器上(在 OSX、virtualenv 上运行)一切正常,但在生产服务器上我收到此错误。在生产服务器上,我在带有 virtualenv 的 Ubuntu 14 上使用 Django 1.7.5。 每个应用都有自己的模板,结构如下:
app1
--templates
----index
------index.html
------head.html views.py app2
--templates
----index
------index.html
------head.html views.py
在 settings.py 我有以下模板参数:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DEBUG = True
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates'),]
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
INSTALLED_APPS = (
# django
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# widgets
'widget_tweaks',
'compressor',
'tinymce',
'django_activeurl',
'debug_toolbar',
# modules
'app1',
'app2',
)
并将它们包含在这样的视图中:
template_event = loader.get_template('index/materials.html', dirs=["app1/templates/"])
【问题讨论】:
-
TemplateDoesNotExist 位于 /app1/1/about/index/index.html。第一个'index'之前有一个空格
-
除非您没有将其包含在示例中,否则您没有名为
index/的目录。 -
但是..在本地服务器上一切正常,我很困惑。
-
我以为问题出在路径上,但是为什么它说该文件存在...
-
在
load_templates中指定dirs是不寻常的。如果您的设置正确,则不需要。使用相对目录'app1/templates/'可能会导致问题。
标签: python django templates loader