[英]os.path python module not working in heroku
我在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尝试按以下顺序加载这些模板:
使用loader 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)
从技术上讲,os.path将指向“项目/项目”,因为这是settings.py所在的位置。 尝试将“模板”目录移到那里。 它为我工作!
只需确保将templateDir更改为以下内容:
templateDir = os.path.dirname(__file__)
TEMPLATE_DIRS = (
os.path.join(templateDir, "templates"),
类似以下内容在类似的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上是否有效。
Windows和* inx系统之间的小差异之一是文件命名。 Windows, winDows, windows, 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.