How do you configure Django for simple development and deployment?
Update: django-configurations has been released which is probably a better option for most people than doing it manually.
If you would prefer to do things manually, my earlier answer still applies:
I have multiple settings files.
-
settings_local.py
- host-specific configuration, such as database name, file paths, etc. -
settings_development.py
- configuration used for development, e.g.DEBUG = True
. -
settings_production.py
- configuration used for production, e.g.SERVER_EMAIL
.
I tie these all together with a settings.py
file that firstly imports settings_local.py
, and then one of the other two. It decides which to load by two settings inside settings_local.py
- DEVELOPMENT_HOSTS
and PRODUCTION_HOSTS
. settings.py
calls platform.node()
to find the hostname of the machine it is running on, and then looks for that hostname in the lists, and loads the second settings file depending on which list it finds the hostname in.
That way, the only thing you really need to worry about is keeping the settings_local.py
file up to date with the host-specific configuration, and everything else is handled automatically.
Check out an example here.
Personally, I use a single settings.py for the project, I just have it look up the hostname it's on (my development machines have hostnames that start with "gabriel" so I just have this:
import socket
if socket.gethostname().startswith('gabriel'):
LIVEHOST = False
else:
LIVEHOST = True
then in other parts I have things like:
if LIVEHOST:
DEBUG = False
PREPEND_WWW = True
MEDIA_URL = 'http://static1.grsites.com/'
else:
DEBUG = True
PREPEND_WWW = False
MEDIA_URL = 'http://localhost:8000/static/'
and so on. A little bit less readable, but it works fine and saves having to juggle multiple settings files.
At the end of settings.py I have the following:
try:
from settings_local import *
except ImportError:
pass
This way if I want to override default settings I need to just put settings_local.py right next to settings.py.