Reputation: 675
Ever since I changed my settings.py
file to be in 3 different files (i.e. base, local and production) within a subfolder 'settings', BASE_DIR
will not display the path to project correctly.
What I want BASE_DIR to output is the following:
`PathToProject/Projectname`
what I am getting (since I moved BASE_DIR in base.py within 'settings' subfolder:
PathToProject/Projectname/Projectname
It bothers me because now it's looking for static
folder within Projectname/Projectname
instead of Projectname
How could I properly configure BASE_DIR function to give me the correct path to project?
Upvotes: 2
Views: 4790
Reputation: 15423
I suggest you to keep your default main settings.py
and make custom ones aside if you need some.
If you need to derivate it, make other settings.py
files for instance for your preproduction version. make your preprod-settings.py
like this, in the same directory that settings.py
:
from .settings import *
DEBUG = False # <-- just add some settings or override the previous ones like this.
This way all your main settings are in the main file, and you only need to put what you want to change in your custom settings files. You'll then be able to change what settings file to use with the manage.py
's --settings
option, or by modifying (or make a custom one) wsgi.py
file in production, with for instance :
import os, sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.mycustomsettingsfile")
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir))
sys.path.append(PROJECT_PATH)
application = get_wsgi_application()
When creating your project, BASE_DIR
's default value is :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
which means :
"BASE_DIR is the parent folder of the parent folder of the file this line is in (settings.py).".
os.path.dirname()
gives the parent folder path, and os.path.abspath()
the absolute path (you need it to give os.path.dirname()
enough informations to get the parent dirnames).
Since BASE_DIR
's value is computed each time you run your server, and since this valus simply depends of the settings.py
location, you don't need custom values for several locations or cases :
You move your project --> BASE_DIR
automatically changes.
If you move your settings.py file, you may have to change the BASE_DIR
value : for instance, if you move it deeper, just add a os.path.dirname
to your BASE_DIR
assignation.
NB : if you want to choose what project to run amongst several subprojects (it seems weird to me though, I'm not sure I understood well. It could ever be wrong, feel free to describe what you want to achieve and why, maybe we could suggest a better way), you could still override the BASE_DIR
value in your custom settings.py
files.
Upvotes: 6