john
john

Reputation: 35390

django config file, how to configure paths based on dev or production environment

How do I configure my settings.py to use a different STATIC_ROOT and STATIC_URL based on whether or not im in a development environment or production environment?

Upvotes: 1

Views: 750

Answers (2)

Jerzyk
Jerzyk

Reputation: 3752

there are several ways of doing this, simplest is to import one more file (usually local_settings.py) and override main settings (production settings are in the main settings.py, local changes in local_settings.py)

code:

try:
    from local_settings import *
except ImportError:
    pass

other option is to keep several separate configuration files like settings_prod.py, settings_dev.py etc. each one having it's own set of configuration settings, but this is a nightmare to keep in sync. however moving parts of the settings to separate files (like conf/db.py, conf/app_data.py, conf/locale.py, conf/logging.py (etc.) and then importing them in the settings files helps a lot. those settings are used by specifying -s or --settings option, with module name as a parameter (without .py part).

i've seen some extension of the second option, that set of settings was automatically chosen depending on the environment variables/path/machine names. so there was one single settings.py with a code to choose which settings to load. this option is nice, so you do not need to specify -s all the time.

last but not least is to use django-admin.py instead of manage.py. difference between those two files are that manage.py is setting a DJANGO_SETTINGS_MODULE environment variable for the specific project. but if you will have e.g. a virtualenv for your development, you can set your local DJANGO_SETTINGS_MODULE to point to the correct settings file, and then use django-admin.py without need to specify config.

switching between projects should be easy too.

i'm sure that there are few more options, but at least you can see what is there :)

Upvotes: 3

Mike Ramirez
Mike Ramirez

Reputation: 10970

Typically I have two settings.py files, local_settings.py and settings.py, the local_settings.py is to set variables that are supposed to be local to the environment. Usually db, paths, debugging settings go in here. This file is never put into version control. There is a local_settings.py.tmpl file that holds the settings that need to be set though.

Then in settings.py I have from local_settings import * Note, settings.py will need to be adjusted if you use INSTALLED_APPS or any of the tuple based settings in the local_settings file. In settings.py change INSTALLED_APPS = () to INSTALLED_APPS += () for example.

Upvotes: 0

Related Questions