3

I'm using a settings module instead of a single settings file for my django project. That means in my wsgi.py file I have something like this:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.prod") or os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.dev")

depending upon my environment. I'd really like to be able to set a variable in a config file so that I can not have to go into this file and change it every time I push to prod.

Not a big deal you're saying? Well it's easy to forget and then boom my app is insecure because it's on dev settings. I would also use this config variable in other places in my app so I have one "global" flag that sets my app to production or dev settings.

1
  • why do you push your wsgi file every time? Commented Jun 12, 2014 at 17:28

2 Answers 2

4

Well how about this approach, create three settings files:

  • settings.py (master)
  • settings_dev.py (override some settings for development server)
  • settings_prod.py (override some settings for production server)

At the end of settings.py file check if DEBUG=True then import settings_dev.py else settings_prod.py:

settings.py:

----
try:
    if DEBUG:
        from settings_dev import *
    else:
        from settings_prod import *
except ImportError:
    pass

Using this approach you will only need to set:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

Now don't say that what if I forgot to change the DEBUG value for specific servers. Then the other solution is that have two separate repositories add settings_prod.py in git ignore list for development repository and add settings_dev.py in git ignore list for production. Always set DEBUG=False in settings_prod.py and DEBUG=True in settings.py file, then at the end of settings.py file do this instead:

try:
    from settings_dev import *
except ImportError:
    try:
        from settings_prod import *
    except ImportError:
        pass
Sign up to request clarification or add additional context in comments.

1 Comment

cool solution:D it would be great though if i could have only one settings file with logic such as if ENV == 'prod': .use these creds. if ENV == "dev': then use these creds. how can this be set up?
0

You could also use an environment variable (or the machine- or user-name) to distinguish different machines. Preferably you set it on the debug-one and never on production, so it can not be forgotten. Something along

if "yes" == os.environ.get("DJANGO_DEBUG_ENABLED"): 
    DEBUG = True
    # or 
    from settings_dev import *
else:
    DEBUG = False
    # or 
    from settings_prod import *

Alternatively you could put a file there which is not part of the repository and check for its presence / absence, or import the DEBUG from a settings_local.py.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.