1

My django project has two environments - development and test. Today I carelessly overwrote the settings.py in test with the one in development. It took me a while to correct the settings in test, which could have been avoided if I have a good way to maintain the two sets of settings separately.

I was thinking to keep two separate copies of settings.py and rename/move them when needed. This, however, is kinda caveman approach. Are there any smarter ways of dealing with this problem?

tamakisquare
  • 16,059
  • 25
  • 84
  • 128
  • possible duplicate of [How to manage local vs production settings in Django?](http://stackoverflow.com/questions/1626326/how-to-manage-local-vs-production-settings-in-django) – ravi404 Aug 10 '15 at 10:28

2 Answers2

3
  1. At the end of your settings.py file, add this:

    try:
        from settings_dev import *
    except ImportError: pass
    

    Where settings_dev.py will contain the dev settings. And in your production env, do not push settings_dev (just ignore it in .gitingore or your source code versioning system.)

    So, when ever settings_dev.py is present, the settings.py will be overwritten by the settings_dev.py file.

  2. One more approach by setting the environment variable:

    if os.environ.get('DEVELOPMENT', None):
        from settings_dev import *
    

    mentioned here: Django settings.py for development and production

I prefer the first one, it's simple and just works.

zengr
  • 37,540
  • 37
  • 125
  • 190
2

Split your settings as documented here:

https://code.djangoproject.com/wiki/SplitSettings#SimplePackageOrganizationforEnvironments

Mehdi Behrooz
  • 2,590
  • 1
  • 15
  • 6