[Fixed]-How to load django debug_toolbar dynamically in your settings

1👍

I believe putting

def show_toolbar(request):
    if DEBUG:
        return True

DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK": show_toolbar,
}

in settings.py is the recommended way and perhaps a bit simpler?

👤Beolap

0👍

settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #conditionally disable later on
    'debug_toolbar',
    #...my apps...

)

#disable if not in DEBUG or if $USE_DEBUG_TOOLBAR is not set.
USE_DEBUG_TOOLBAR = bool(int(os.getenv("USE_DEBUG_TOOLBAR", 0))) and DEBUG

#disable as well if running unit tests...
pgm = os.path.basename(sys.argv[0])
if not USE_DEBUG_TOOLBAR or pgm.startswith("test") or pgm.startswith("nosetests"):
    li = [app for app in INSTALLED_APPS if not app == "debug_toolbar"]
    INSTALLED_APPS = tuple(li)

and your command line use might look like:

export USE_DEBUG_TOOLBAR=1  && python manage.py runserver

Leave a comment