Inspired during DjangoCon 2011. After creating six projects to test during the talks, I realized that setting up the settings.py file to be more modular was a real pain. I wrote this little script to automate the process.
Django's default way of doing settings kind of sucks. Adding:
try: from local_settings import * except ImportError: pass
to the bottom of the settings.py works, but it doesn't allow you to extend settings. So, for example, you can't add django-debug-toolbar to just your local development without overriding the entire INSTALLED_APPS tuple.
What this script does:
- Read in your current settings.
- Creates a new folder named settings within the project directory.
- Writes your current settings to base.py within the new settings package.
- Creates a basic __init__.py file which imports base.py and looks for local.py
- Creates an empty local.py for your dev environment.
- Deletes the default settings.py (and settings.pyc, should it exist).
What You Get
Now you can edit local.py for your dev environment and extend things like INSTALLED_APPS but just for you.
INSTALLED_APPS += ( 'debug_toolbar', ) MIDDLEWARE_CLASSES += ( 'debug_toolbar.middleware.DebugToolbarMiddleware', ) INTERNAL_IPS = ('127.0.0.1',)
This is a simple script that does not need to be installed or compiled. It's a run-once-and-discard project. You will want to curl or wget the modular_settings.py file to your project directory. Specifically to the directory where the default Django settings.py file resides.
curl -O https://raw.github.com/chrisjones-brack3t/django-modular-settings/master/modular_settings.py
Or, if you prefer wget:
After downloading the file, run the script from your project directory.
Assuming everything goes as planned, you should see a message saying your settings are now a little more awesome.
Delete the modular_settings.py file.