【问题标题】:BASE_DIR returning settings path and not project path (django 1.10)BASE_DIR 返回设置路径而不是项目路径(django 1.10)
【发布时间】:2017-02-28 10:34:20
【问题描述】:

我目前正在根据两个 Scoops For Django 1.8 首选设置文件设置为 Django 1.10 设置我的设置文件。

我的 base.py 设置文件是:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

BASE_DIR 正在返回以下路径:

/Devs/projects/captain_log/src/cap_log

我的文件树:

|--Virtual Env Folder
    |--src(django project)/
       |--cap_log(django config)/
          |--init.py
          |--urls.py
          |--wsgi.py
          |--settings/
              |-- init.py
              |-- base.py (all settings located here)
              |-- development.py
              |-- production.py
              |-- etc.

我假设 BASE_DIR 应该返回:

/Devs/projects/captain_log/src/

我问是因为我的 STATIC_DIRS 也返回了:

/Devs/projects/captain_log/src/cap_log/static

代替:

 /Devs/projects/captain_log/src/static

有人可以就我正在做的事情提出解决方案或更正建议。 它还影响模板路径、collectstatic、媒体路径等。

【问题讨论】:

    标签: django django-settings django-1.10


    【解决方案1】:

    再试一次dirname电话

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

    第一个目录名给你设置,第二个给你配置文件夹,第三个会把你放在父目录中

    __file__ # is the current file location
    os.path.abspath(__file__) # locates you on the file structure
    os.path.dirname(os.path.abspath(__file__)) # gives you the directory of the file at the supplied filepath
    

    默认假设是您使用的是settings.py 文件而不是目录,因此您是原始配置中的一个浅目录

    【讨论】:

    • 谢谢。那行得通。我不知道为什么我没有想到这一点。
    • 我将我的 settings.py 文件移动到一个 settings/ 文件夹中以保存我所有的设置文件。一切都乱了套。在现有值上再添加一个os.path.dirname 为我修复了它。 BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2015-03-19
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多