Skip to content

Instantly share code, notes, and snippets.

@msabramo
Created April 27, 2011 22:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msabramo/945406 to your computer and use it in GitHub Desktop.
Save msabramo/945406 to your computer and use it in GitHub Desktop.
How to load (optional) Django apps only if they're present (from http://blog.jupo.org/post/586256417/optional-django-apps)
LOAD_OPTIONAL_APPS = True
if LOAD_OPTIONAL_APPS:
# <copypaste from="http://blog.jupo.org/post/586256417/optional-django-apps">
# Define any settings specific to each of the optional apps.
#
import sys
USE_SOUTH = not (len(sys.argv) > 1 and sys.argv[1] == "test")
DEBUG_TOOLBAR_CONFIG = {"INTERCEPT_REDIRECTS": True}
# Sequence for each optional app as a dict containing info about the app.
OPTIONAL_APPS = (
{"import": "django_extensions", "apps": ("django_extensions",)},
{"import": "debug_toolbar", "apps": ("debug_toolbar",),
"middleware": ("debug_toolbar.middleware.DebugToolbarMiddleware",)},
{"import": "south", "apps": ("south",), "condition": USE_SOUTH},
)
# Set up each optional app if available.
for app in OPTIONAL_APPS:
if app.get("condition", True):
try:
__import__(app["import"])
except ImportError:
pass
else:
INSTALLED_APPS += app.get("apps", ())
MIDDLEWARE_CLASSES += app.get("middleware", ())
TEMPLATE_CONTEXT_PROCESSORS += app.get("context_processors", ())
#
# </copypaste>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment