Heroku Django,templatedoesnotexist错误 [英] Heroku Django, templatedoesnotexist error
问题描述
因此,在经过了深夜的工作之后,我终于将我的应用程序部署到了Heroku上,但现在又出现了一个问题,就是不眠之夜,该模板不存在</p>
错误, 我正在使用Django.1.11,因此我的设置如下;
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
'photos',
]
我的模板如下所示,从文档中开始,已安装的应用(APP_DIR设置为true)将在应用内查找template文件夹.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
最后是我的应用程序结构;
| mysite
|照片
---- |模板
-------- |照片
------------ | index.html
当我加载页面时,我可以从日志中看到;
Using engine django:
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/templates/photos/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/templates/photos/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/photos/templates/photos/index.html (Source does not exist)
最后一行显示了正确的路径,但是不知何故,我真的不知道为什么,有人可以照亮!
谢谢 吉米
如果要显式添加模板文件夹的路径,则可以这样设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR,'photos/templates/photos/templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
So after couple late night works, I finally had my app deployed onto the Heroku, but now a different issue, and sleepless night, the template does not exist
error, I'm using Django.1.11, so my setting is as following;
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
'photos',
]
and my TEMPLATES is as following, as from the doc, the installed app, with APP_DIR set to be true, would look for the templates folder within the apps.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
And finally my app structure;
|mysite
|photos
----|templates
--------|photos
------------|index.html
when I load the page, I can see from the log;
Using engine django:
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/templates/photos/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/templates/photos/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/photos/templates/photos/index.html (Source does not exist)
The last line, shows the correct path, but somehow, it cannot be found, I really dont know why, can someone shed some lights !
Thanks Jimmy
If you want to add explicitly the path for the template folder, you can set it like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR,'photos/templates/photos/templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
这篇关于Heroku Django,templatedoesnotexist错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!