[Django]-Django Debug Toolbar show queries from middleware

3👍

According to the documentation:

The order of MIDDLEWARE_CLASSES is important. You should include the
Debug Toolbar middleware as early as possible in the list. However, it
must come after any other middleware that encodes the response’s
content, such as GZipMiddleware.

The solution is to put debug_toolbar.middleware.DebugToolbarMiddleware before your custom middleware in MIDDLEWARE_CLASSES.

👤alecxe

0👍

One of the place i found error is when I added a separate file debugtoolbar.py for all settings pertaining to enable debug_toolbar. I just imported this in my settings_local.py but it was calling somehow twice and it was not showing the queries.

As soon as I added a conditional statement to add

import os
import sys

from my_project.settings import INSTALLED_APPS, MIDDLEWARE_CLASSES

DEBUG_TOOLBAR_APPS_NAME = 'debug_toolbar'

if DEBUG_TOOLBAR_APPS_NAME not in INSTALLED_APPS:
    INSTALLED_APPS += ( DEBUG_TOOLBAR_APPS_NAME, )

DEBUG_TOOLBAR_MIDDLEWARE = 'debug_toolbar.middleware.DebugToolbarMiddleware'
if DEBUG_TOOLBAR_MIDDLEWARE not in MIDDLEWARE_CLASSES:
    MIDDLEWARE_CLASSES = ( DEBUG_TOOLBAR_MIDDLEWARE, ) + MIDDLEWARE_CLASSES

def custom_show_toolbar(request):
    return True

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': 'my_project.settings.custom_show_toolbar',
}

It started working all fine. Hope this will save someone’s time.

👤PiyusG

Leave a comment