Heroku无法找到Django模板 [英] Heroku can't find Django templates
问题描述
当Heroku查找我的html文件时,出现 TemplateDoesNotExist
错误。这些文件都在开发服务器上同步。 TEMPLATE_DIRS
设置设置为:
I'm getting a TemplateDoesNotExist
error on Heroku when it looks for my html files. The files all sync up on the development server. The TEMPLATE_DIRS
setting is set to:
TEMPLATE_DIRS = ['/Users/jonathanschen/Python/projects/skeleton/myportfolio/templates',]
但是当试图加载页面herokuapp页面我收到以下错误:
我认为这里有一些非常基本的东西我在这里失踪。
But when trying to load the page the herokuapp page I get the following error: I think there's something very basic I'm missing here.
TemplateDoesNotExist at /
index.html
Request Method: GET
Request URL: http://morning-coast-2859.herokuapp.com/
Django Version: 1.4.1
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: /app/.heroku/venv/lib/python2.7/site-packages/django/template/loader.py in find_template, line 138
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/jonathanschen/Python/projects/skeleton/myportfolio/templates/index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/app/.heroku/venv/lib/python2.7/site-packages/django/contrib/auth/templates/index.html (File does not exist)
/app/.heroku/venv/lib/python2.7/site-packages/django/contrib/admin/templates/index.html (File does not exist)
推荐答案
您需要将TEMPLATE_DIRS设置更新为指向Heroku可以找到 - 你现在设置的路径将在本地工作,但Heroku不知道 / Users / jonathanschen /
的位置(因为它没有夹)。您可能想尝试使TEMPLATE_DIRS设置使用相对路径:
You'll need to update your TEMPLATE_DIRS setting to point to something Heroku can find - the path that you have it set to right now will work locally, but Heroku has no idea where /Users/jonathanschen/
is (because it doesn't have that folder). You might want to try making your TEMPLATE_DIRS setting use a relative path:
import os.path
PROJECT_DIR = os.path.dirname(__file__) # this is not Django setting.
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, "templates"),
# here you can add another templates directory if you wish.
)
(from http://www.djangofoo.com/35/template_dirs-project-folder )
在Django 1.8+中,更改 DIRS
选项在 TEMPLATES
代替:
In Django 1.8+, change the DIRS
option in TEMPLATES
instead:
# BASE_DIR should already be in settings
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
...
}
]
这篇关于Heroku无法找到Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!