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 can I repair my git configuration?
  • How to manage differences using same code base for multiple Rails 2.3 websites
  • Domain name change on Git Server
  • Missing (obvious) buttons for Git in Netbeans
  • Force Git Pull to fail if a file is locked by another process
  • How do I use Notepad++ (or other) with msysgit?
  • git: Retroactively introduce several merges
  • Connection error with private repository using GitVersion as a build step in TeamCity
  • How can I recover from 'fatal: reference is not a tree: master'?
  • Cannot Remove Tracked File From Git Bitbucket Repo
  • private branch on public repository bitbucket
  • Is “tags” a reserved keyword 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.