Django settings.py: Separate local and global configuration
I was wondering if it was possible to separate the “local” configuration in Django (Local path to static, templates content which have to be absolute, local DB info, etc…) from the “global” configuration (URL, Middleware classes, installed apps, etc…) so that several people can work on a same project over Git or SVN without having to rewrite the local settings every time there is a commit done!
2 Solutions collect form web for “Django settings.py: Separate local and global configuration”
Yes, definitely. The settings.py file is just Python, so you can do anything in there – including setting things dynamically, and importing other files to override.
So there’s two approaches here. The first is not to hard-code any paths, but calculate them dynamically.
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) TEMPLATE_DIRS = [ os.path.join(PROJECT_ROOT, "templates"), ]
etc. The magic Python keyword
__file__ gives the path to the current file.
The second is to have a
local_settings.py file outside of SVN, which is imported at the end of the main settings.py and overrides any settings there:
try: from local_settings import * except ImportError: pass
The try/except is to ensure that it still works even if local_settings is not present.
Naturally, you could try a combination of those approaches.
You can split up your configuration into different files. As they are written in python you’d just import the settings from the other settings file using
import local_settings you even can put the import in a conditional to import local settings depending on some context.
Take a look at the documentation: http://docs.djangoproject.com/en/dev/topics/settings/