0

For a number of reasons, running from unit testing performance to migration issues (Django Migration Error: Column does not exist), I have found it useful to turn the debug toolbar on and off.

Here's a way I've found to control loading it from environment variables.

No, not really a question, think of it as a recipe that I wish I had found on SO.

Community
  • 1
  • 1
JL Peyret
  • 10,917
  • 2
  • 54
  • 73

2 Answers2

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
  • 768
  • 9
  • 15
  • 1
    OK, accept, but keep in mind that `show_toolbar` would be driven by my environment variable instead - I've had enough problems with this tool in the past that I rarely use it, even in a development (DEBUG = True) environment. – JL Peyret Jul 02 '21 at 03:52
  • Yes, of course, I just left that out for simplicity! – Beolap Jul 02 '21 at 06:11
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
JL Peyret
  • 10,917
  • 2
  • 54
  • 73