我最近将 Django 从 v1.3.1 升级到 v1.4。
在我以前的 settings.py
我有
TEMPLATE_DIRS = (
os.path.join(os.path.dirname( __file__ ), 'templates').replace('\\', '/'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
这将指向 /Users/hobbes3/Sites/mysite/templates
, 但是 because Django v1.4 moved the project folder to the same level as the app folders , 我的 settings.py
文件现在位于 /Users/hobbes3/Sites/mysite/mysite/
而不是 /Users/hobbes3/Sites/mysite/
.
所以实际上我现在的问题是双重的:
os.path
从 __file__
查看上一级目录.换句话说,我想要 /Users/hobbes3/Sites/mysite/mysite/settings.py
查找 /Users/hobbes3/Sites/mysite/templates
使用相对路径。template
项目 admin
中的文件夹(具有跨应用模板,如 registration
、 /User/hobbes3/Sites/mysite
等)水平或在 /User/hobbes3/Sites/mysite/mysite
?最佳答案
os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'templates'))
至于模板文件夹应该去哪里,我不知道,因为 Django 1.4 刚出来,我还没有看。您可能应该在 SE 上提出另一个问题来解决该问题。
您也可以使用normpath
来清理路径,而不是abspath
。但是,在这种情况下,Django 需要的是绝对路径而不是相对路径。
为了跨平台兼容性,请使用 os.pardir
而不是 '..'
。
https://stackoverflow.com/questions/9856683/
相关文章:
python - `ValueError: cannot reindex from a duplic
linux - 如何在 Linux 上获得整体 CPU 使用率(例如 57%)
python - 使用 Python 向 RESTful API 发出请求
linux - 如何在linux中设置环境变量LD_LIBRARY_PATH
python - 从 pandas.DataFrame 中选择复杂的标准
python - 如何在 Django 中的 CharField 上添加占位符?
linux - 我可以用/etc/hosts 映射一个主机名*和*一个端口吗?