You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django 3.1.4生产环境配置中设置BASE_DIR?

How to Adjust BASE_DIR with Path in New Django Versions for Your Folder Structure

Got it, let's break this down clearly.

First, let's recap the context: Your folder structure has an extra layer of nesting compared to Django's default setup—this is why you had to add an extra os.path.dirname() in the old-style BASE_DIR definition.

Default New Django BASE_DIR Setup

The standard Path-based BASE_DIR in modern Django looks like this:

BASE_DIR = Path(__file__).resolve().parent.parent

Here's what each part does:

  • Path(__file__): Points directly to your production.py file
  • .resolve(): Gets the absolute, cleaned-up path of that file
  • First .parent: Moves up one directory to your settings folder (where production.py lives)
  • Second .parent: Moves up another directory to your project's core folder (the one containing the settings directory)

Your Modified Path-Based Setup

Since you needed an extra os.path.dirname() in the old method, you just need to add one more .parent to the Path-based code. The correct line for your folder structure is:

BASE_DIR = Path(__file__).resolve().parent.parent.parent

That third .parent moves up one additional level to reach your project's root directory—the BASE_DIR you need for your specific structure.

Quick Way to Verify

To double-check you've got the right path, you can temporarily add a print statement:

print(BASE_DIR)

Then run a command like python manage.py shell (any command that loads your settings will work) and confirm the output matches your project's root folder path.

内容的提问来源于stack exchange,提问作者Aakash Solanki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:44:56