【问题标题】:Heroku can't find Django templatesHeroku 找不到 Django 模板
【发布时间】:2012-08-01 21:54:24
【问题描述】:

我在 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)

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    您需要更新您的 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")],
            ...
        }
    ]
    

    【讨论】:

    • 嘿 girasquid 感谢您的回复,我尝试使用您建议的相对路径更新设置,但由于某种原因它仍然找不到模板。
    • 它看起来在这里:Django 尝试按以下顺序加载这些模板:使用加载程序 django.template.loaders.filesystem.Loader:/app/myportfolio/templates/index.html(文件不存在)使用加载器 django.template.loaders.app_directories.Loader:/app/.heroku/venv/lib/python2.7/site-packages/django/contrib/auth/templates/index.html(文件不存在)/app/ .heroku/venv/lib/python2.7/site-packages/django/contrib/admin/templates/index.html(文件不存在)
    • @girasquad:你能在本地运行应用吗?
    • 感谢@gira,我将模板文件夹放在根目录中,而不是在应用程序目录中
    • @Jonathan 我现在遇到了这个问题。我有 import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) TEMPLATE_DIRS = ( os.path.join(os.path.dirname(BASE_DIR), "myapp", "static", "templates"), ) 但我遇到了同样的错误。你能帮帮我吗?
    猜你喜欢
    • 2021-04-12
    • 2011-12-23
    • 2018-06-26
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2012-07-10
    相关资源
    最近更新 更多