【问题标题】:os.path python module not working in herokuos.path python模块在heroku中不起作用
【发布时间】:2012-06-28 18:15:53
【问题描述】:

我正在 heroku 上构建一个 django 应用程序,但在使用 os.path 模块时遇到了很多麻烦。我的项目无法在 heroku 上找到 templates,而它在 localhost 上运行良好。

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

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)

【问题讨论】:

  • 为什么你的模板文件夹中有css和media?
  • @ArgsKwargs 它不在包含模板的文件夹中。它就在名为“模板”的文件夹中。我知道它有点混乱。
  • @ArgsKwargs 我认为这个问题不应该打扰

标签: python django heroku django-templates


【解决方案1】:

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

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

    templateDir = os.path.dirname(__file__) 
    TEMPLATE_DIRS = (
    os.path.join(templateDir, "templates"),

【讨论】:

    【解决方案2】:

    在类似的 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 是outside that directory。我可以验证这在 Heroku 上是否有效。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-01
        • 2011-09-30
        • 1970-01-01
        • 1970-01-01
        • 2011-10-14
        • 2020-07-12
        • 1970-01-01
        相关资源
        最近更新 更多