Python - Making a script that changes Django settings

Viewed 301

I have a django project with 3 completely different templates, each with its own folder. Would it be possibile to make a python script that changes django's settings ( file paths to static files and templates ) when executed ?

Can anyone point me to a tutorial or give me advice on how would I go about achieving this on my own ?

Thanks !

2 Answers

Instead of:

settings.py

You can have a structure like this:

settings/
  base.py
  development.py
  staging.py
  production.py

base.py contains all the common settings.


development.py imports all the settings from base.py and overwrites necessary settings for the development server something like this:

from your_app.settings.base import *

A_SETTING_TO_CHANGE = DIFFERENT_VALUE_FOR_DEVELOPMENT

staging.py imports all the settings from base.py and overwrites necessary settings for the staging server something like this:

from your_app.settings.base import *

A_SETTING_TO_CHANGE = DIFFERENT_VALUE_FOR_STAGING

production.py imports all the settings from base.py and overwrites necessary settings for the production server something like this:

from your_app.settings.base import *

A_SETTING_TO_CHANGE = DIFFERENT_VALUE_FOR_PRODUCTION

Then in wsgi.py and manage.py change:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings')

To:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings.development')

  • In the staging server, set the DJANGO_SETTINGS_MODULE environment variable to 'your_app.settings.staging'.

  • In the production server, set the DJANGO_SETTINGS_MODULE environment variable to 'your_app.settings.production'.

Related