python - Heroku 找不到 Django 模板

标签 python django heroku

当 Heroku 查找我的 html 文件时,我收到一个 TemplateDoesNotExist 错误。这些文件都在开发服务器上同步。 TEMPLATE_DIRS 设置为:

TEMPLATE_DIRS = ['/Users/jonathanschen/Python/projects/skeleton/myportfolio/templates',]

但是当尝试加载 herokuapp 页面时,出现以下错误: 我认为这里缺少一些非常基本的东西。

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 设置使用相对路径:

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.
)

(来自 http://www.djangofoo.com/35/template_dirs-project-folder)

在 Django 1.8+ 中,更改 TEMPLATES 中的 DIRS 选项:

# 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")],
        ...
    }
]

关于python - Heroku 找不到 Django 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11768143/

相关文章:

heroku - 我从哪里获得 PyTorch 的仅 CPU 版本?

json - 无法使用 Firebase 和 ElasticSearch(使用 Flashlight)进行正确的路径监控或查询

git - 如何使用heroku查看整个提交历史?

python - 为什么在 if __name__ == "__main__"下使用 manage.py 执行脚本会运行两次

python - Django 后台任务 vs Celery

python - 在 Amazon Web Services 上创建 cron 作业的可扩展方式是什么?

python - 排序二维列表python

python - 在python中反序列化objective-c二进制NSMutableArray

python - 如何在 urwid 中创建嵌套列表框?

python - 导入错误: cannot import name IPossibleSitenterfaces import IPossibleSite: version mismatch?