3

I am using Django Debug Toolbar to debug and optimize my website. I am also using some custom middleware to do things such as checking to see if a user is logged in and is allowed to access the url they are trying to view, querying ads, etc.

In this middleware, sometimes SQL queries are performed, but the queries don't show up under the 'queries' panel in DDT. Is there any way to get DDT to recognize and track middleware?

Hat
  • 1,691
  • 6
  • 28
  • 44

2 Answers2

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
  • 462,703
  • 120
  • 1,088
  • 1,195
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
  • 1,157
  • 16
  • 28
  • 1
    This certainly did save me time, thank you. I reduced it to: `DEBUG_TOOLBAR_CONFIG = { 'SHOW_TOOLBAR_CALLBACK': lambda x: True, }` – Paul M Furley Dec 21 '17 at 12:03