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.

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Yes, of course, I just left that out for simplicity!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.