Django settings.py: separate local and global configuration

I was wondering if it is possible to separate the “local” configuration in Django (the local path to the static, the content of the templates, which should be absolute, the local database information, etc.) from the “global” configuration (URL, Middleware classes, installed applications etc.), so that several people can work on the same project on top of Git or SVN, without overwriting the local settings every time a commit is made!

Thanks!

+3
source share
2 answers

Yes, definitely. The settings.py file is just Python, so you can do anything you like, including dynamically adjusting and import other files for redefinition.

So, there are two approaches. The first is not to hard-code any paths, but to compute them dynamically.

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) TEMPLATE_DIRS = [ os.path.join(PROJECT_ROOT, "templates"), ] 

etc .. The Python magic keyword __file__ indicates the path to the current file.

The second is the presence of the local_settings.py file outside SVN, which is imported at the end of the main settings.py parameters and overrides any settings there:

 try: from local_settings import * except ImportError: pass 

Try / except is that it works even if local_settings does not exist.

Naturally, you could try a combination of these approaches.

+12
source

You can split the configuration into different files. Since they are written in python, you simply import the settings from another settings file using import local_settings , you can even put the import into a conditional one to import local parameters depending on some context.

Take a look at the documentation: http://docs.djangoproject.com/en/dev/topics/settings/

0
source

All Articles