0

我正在 heroku 上构建一个 django 应用程序,并且在os.path模块方面遇到了很多麻烦。我的项目在本地主机上完美运行时无法templates在 heroku 上找到它。

这是我的项目层次结构(简而言之):

project/
        project/
               settings.py
               urls.py
               views.py
               ..
        manage.py
        templates/
                 css/
                 media/
                 Templates/
                          home.html

所以,我曾经os.path在settings.py中添加模板目录。

currDir = os.path.dirname(__file__)
templateDir = os.path.join(os.path.join(os.path.split(currDir)[0], "templates"), "templates")
TEMPLATE_DIRS = (
    templateDir,

)

这在我的本地主机上运行良好,但在 Heroku 上运行不正常。

以下是在heroku上提到的(在heroku上运行)

Django 尝试按以下顺序加载这些模板:

使用加载器 django.template.loaders.filesystem.Loader:

/app/templates/templates/home.html (File does not exist)

*使用加载器 django.template.loaders.app_directories.Loader:*

/app/.heroku/venv/lib/python2.7/site-packages/django/contrib/auth/templates/home.html(File does not exist)
4

3 回答 3

2

从技术上讲,os.path 将指向“项目/项目”,因为这是 settings.py 所在的位置。尝试将您的“模板”目录移动到那里。它对我有用!

只需确保将 templateDir 更改为以下内容:

    templateDir = os.path.dirname(__file__) 
    TEMPLATE_DIRS = (
    os.path.join(templateDir, "templates"),
于 2012-10-28T16:33:27.683 回答
1

在类似的 Heroku/MEDIA_ROOT 问题上,以下内容对我有用。

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

BASE_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
    os.path.join(BASE_PATH, "project/templates/templates"), 
)

但是,如果您坚持使用 Django 的默认目录结构,则根本不必设置 TEMPLATE_DIRS。即,home.html应该在project/project/templates. 通常 css/javascript位于该目录之外。我可以验证这适用于 Heroku。

于 2012-06-28T20:32:30.923 回答
1

Windows 和 *inx 系统之间的小区别之一是文件命名。 Windows, winDows, windows, windowS表示windows下的同一个文件,Linux下不行。

这是我在使用 Heroku 时遇到的问题(可能是在 *inx 上)。所以,我不得不在 TEMPLATE_DIRS 中使用准确的文件夹名称。

这是正确的。

templateDir = os.path.join(os.path.join(os.path.split(currDir)[0], "templates"), "Templates")

上一个是:

templateDir = os.path.join(os.path.join(os.path.split(currDir)[0], "templates"), "templates")
于 2012-06-29T03:26:53.587 回答