Django Debugging Using Django Debug Toolbar


Example

First, you need to install django-debug-toolbar:

pip install django-debug-toolbar

settings.py:

Next, include it to project's installed apps, but be careful - it's always a good practice to use a different settings.py file for such development-only apps and middlewares as debug toolbar:

# If environment is dev...
DEBUG = True

INSTALLED_APPS += [
    'debug_toolbar',
]

MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware']

Debug toolbar also relies on static files, so appropriate app should be included as well:

INSTALLED_APPS = [
    # ...
    'django.contrib.staticfiles',
    # ...
]

STATIC_URL = '/static/'

# If environment is dev...
DEBUG = True

INSTALLED_APPS += [
    'debug_toolbar',
]

In some cases, it's also required to set INTERNAL_IPS in settings.py:

INTERNAL_IPS = ('127.0.0.1', )

urls.py:

In urls.py, as official documentation suggests, the next snippet should enable debug toolbar routing:

if settings.DEBUG and 'debug_toolbar' in settings.INSTALLED_APPS:
    import debug_toolbar
    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]

Collect toolbar's static after installation:

python manage.py collectstatic

That's it, debug toolbar will appear on you project's pages, providing various useful information about execution time, SQL, static files, signals, etc.

HTML:

Also, django-debug-toolbar requires a Content-type of text/html, <html> and <body> tags to render properly.


In case if you sure you've configured everything right, but debug toolbar is still not rendered: use this "nuclear" solution to try to figure it out.