47
Grab the __file__
global, and use the various functions in os.path
on it.
import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
109
The architecture of a project in Django
-
root/
- app1/
- app2/
- …
- main/
- settings.py
Inside settings.py:
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
-> gives the path of the file settings.py: root/main/. This is NOT THE ROOT OF THE PROJECT
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
-> gives the root of the project: root/. This is THE ROOT OF THE PROJECT.
- [Django]-Django admin – inline inlines (or, three model editing at once)
- [Django]-Overriding the save method in Django ModelForm
- [Django]-What's the best solution for OpenID with Django?
71
Django 1.8 already includes the project root directory as BASE_DIR
:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
And you can use it in your app by importing settings
:
from django.conf import settings
...
...
print(settings.BASE_DIR)
- [Django]-Django 1.7 – makemigrations not detecting changes
- [Django]-Django: "projects" vs "apps"
- [Django]-Django create userprofile if does not exist
- [Django]-How to unit test file upload in django
- [Django]-What is the advantage of Class-Based views?
- [Django]-In Django – Model Inheritance – Does it allow you to override a parent model's attribute?
Source:stackexchange.com