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!

Thanks!

  • How to set the PATH environment variable in Jenkins configuration on Windows?
  • Version-controlled extension configuration in Mercurial
  • maintaining configuration differences between dev and live environments during deployment from SVN
  • Where should environment-specific values be saved in a Laravel 5 application?
  • Managing user configuration files across multiple computers
  • Is there a way to override a git author's display name in local repository config?
  • Github Private Team Separate SSH Keys
  • Working tree vs working directory
  • How can I move commits from the trunk to a branch in Git?
  • Git apply 3way error “repository lacks the necessary blob to fall back on 3-way merge.”
  • How can I create a gitsubtree of an existing repository?
  • Difference between prunings in git
  • 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/

    Git Baby is a git and github fan, let's start git clone.