4

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 !

1
  • 1
    Why not use multiple settings files? You can have a base_settings.py which has common settings and then separate files with the settings that you want. After that, you would just import everything from the desired settings file into base_settings.py. You could even take it further and make it import a specific file based on an ENV variable. Commented Oct 13, 2019 at 16:55

2 Answers 2

2

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'.

Sign up to request clarification or add additional context in comments.

Comments

0

A solution using a non-version controlled local_settings.py file (which is imported into settings.py and overrides values) is outlined in this post:

https://agileleaf.com/blog/a-better-way-to-manage-settings-py-in-your-django-projects/amp/

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.