Adventures in Freelancing
Django How-To: Add Debugging Apps to Your Local Settings
There are some great debugging apps, like the django-debug-toolbar and django-command-extensions, that help out immensely while developing your next killer Django site. There’s a small catch to using them though, which is that they have to be added to INSTALLED_APPS. No big deal, but you don’t really want these installed and running in a production install.
Again, no big deal. It’s an old trick now to maintain a separate local_settings.py file. By importing our local_settings at the end of the settings.py module, anything in local_settings will overwrite whatever is in settings.
So all we have to do then is make a new list of debugging apps in local_settings.py, and add them to the INSTALLED_APPS list in settings.py.
My first naive attempt was to do this:
# local_settings.py
INSTALLED_APPS += ('django_extensions',)
Can you spot the problem? The original INSTALLED_APPS is not available in local_settings.py, so we can’t add to it there.
Well, if we can’t add our list in local_settings.py, let’s add in in settings.py
# local_settings.py
DEBUG_APPS = ('django_extensions',)
# settings.py
try:
from local_settings.py import *
INSTALLED_APPS += DEBUG_APPS
except ImportError:
pass
This works just fine, as long as there is a DEBUG_APPS list specified in local_settings.py. However, the whole point of having a local_settings.py file is to optionally add or overwrite settings.
To remove this dependency, simply define an empty DEBUG_APPS setting in the main settings.py file.
Our end result looks like:
# local_settings.py
DEBUG_APPS = ('django_extensions',)
# settings.py
DEBUG_APPS = None
try:
from local_settings.py import *
INSTALLED_APPS += DEBUG_APPS
except ImportError:
pass
Another nice trick, if you’re using pip, is to set up a separate requirements file for your debugging apps. For example, a requirements file for django_extensions:
# debug_requirements.txt django-extensions==0.5 Werkzeug==0.6.2
Then to install my debugging apps, I just run pip install -r debug_requirements.txt
| Print article | This entry was posted by clawlor on June 3, 2010 at 10:16 am, and is filed under Django. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |