Django,TemplateDoesNotExist错误:Heroku找不到模板目录。如何展示它在哪里看?

时间:2013-02-20 22:30:09

标签: django templates heroku django-templates

我做了很多点击和syncdb'ing并且还没有找到解决方案。我收到这个错误:

TemplateDoesNotExist at /quotes/
quotes/index.html
Request Method: GET
Request URL:    http://quoteboard94.herokuapp.com/quotes/
Django Version: 1.4.4 
Exception Type: TemplateDoesNotExist
Exception Value:    
quotes/index.html
Exception Location: /app/.heroku/python/lib/python2.7/site-packages/django/template/loader.py in find_template, line 138
Python Executable:  /app/.heroku/python/bin/python
Python Version: 2.7.3
Python Path:    
['/app',
 '/app/.heroku/python/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
 '/app',
 '/app/.heroku/python/lib/python27.zip',
 '/app/.heroku/python/lib/python2.7',
 '/app/.heroku/python/lib/python2.7/plat-linux2',
 '/app/.heroku/python/lib/python2.7/lib-tk',
 '/app/.heroku/python/lib/python2.7/lib-old',
 '/app/.heroku/python/lib/python2.7/lib-dynload',
 '/app/.heroku/python/lib/python2.7/site-packages',
 '/app/.heroku/python/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

我的设置有:

PROJECT_DIR = os.path.dirname(__file__) #for heroku

TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, '/templates/'), # for heroku
    '/Users/jacksongonzales/projects/QuoteBoard/templates/',
)

import os.path也是最重要的。

TEMPLATE_DIRS下的路径是模板的绝对路径。

我是不是在为PROJECT_DIR和TEMPLATE_DIRS变量做正确的事情?

2 个答案:

答案 0 :(得分:3)

我认为应该是

os.path.join(PROJECT_DIR, 'templates'), # for heroku

没有斜线。

os.path.join(PROJECT_DIR, '/templates/'), # for heroku

返回/templates/而不是您期望的路径

来自docs

  

如果任何组件是绝对路径,则所有先前组件(打开   Windows,包括之前的驱动器号,如果有的话)   扔掉了,继续加入。

答案 1 :(得分:2)

这是我为heroku项目设置的。

# here() gives us file paths from the root of the system to the directory
# holding the current file.
here = lambda * x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)

PROJECT_ROOT = here("..")
# root() gives us file paths from the root of the system to whatever
# folder(s) we pass it starting at the parent directory of the current file.
root = lambda * x: os.path.join(os.path.abspath(PROJECT_ROOT), *x)

....

TEMPLATE_DIRS = (
    root('templates'),
)

更新: 我的项目与其部署的项目具有相同的本地模板结构,因此我不需要在模板目录中添加直接路径。你有一个在你的:

'/Users/jacksongonzales/projects/QuoteBoard/templates/',

如果您TEMPLATE_DIRS中的第一个条目是正确的,那么您是否需要第二个条目?