Django:在 os.path.join(...) 命令之后 BASE_DIR 不正确

问题描述 投票:0回答:2

我在 Django 中遇到以下行为:

当我在其上使用“

os.path.join(...)”命令时,BASE_DIR 似乎发生了变化。

我的settings.py文件:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

在 Python Shell 中:

>>> import os
>>> from django.conf import settings

>>> base_dir = settings.BASE_DIR
***'C:\\Users\\gille\\timeless_wisdom'***

>>> file_path = os.path.join(base_dir, '/timeless_wisdom/UserData')
***'C:/timeless_wisdom/UserData'***

所以:当我用BASE_DIR加入相对路径时,我没有得到预期的结果,但他又从C:/开始...

我尝试了以下方法,但结果相同。:

file_path = os.path.join(base_dir, '\\timeless_wisdom\\UserData')

我尝试使用 PROJECT_ROOT 而不是 BASE_DIR,但结果相同。

我有什么遗漏的吗?谢谢...

django python-3.x path settings environment
2个回答
4
投票

路径上不应有任何前导斜杠。

file_path = os.path.join(base_dir, 'timeless_wisdom/UserData')

0
投票

SESSION_ENGINE = 'django.contrib.sessions.backends.file'

SESSION_FILE_PATH = BASE_DIR / '会话'

© www.soinside.com 2019 - 2024. All rights reserved.