Django Debug Toolbar not displaying SQL
Asked Answered
C

5

10

I recently installed django-debug-toolbar. The toolbar works and I can see the tabs on the side. However, nothing shows up in the SQL tab even when I have obviously executed an SQL query (such as in the admin): enter image description here

My settings are as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2'
        'NAME': 'mydatabase'
         ....
    }
}

# Backwards compatability with apps
DATABASE_ENGINE = DATABASES['default']['ENGINE'].split('.')[-1]
DATABASE_NAME = DATABASES['default']['NAME']

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
)

INSTALLED_APPS = (
    ...
    'debug_toolbar',
    ...
)

# Settings for the django-debug-toolbar
DEBUG_TOOLBAR_PANELS = (
    'debug_toolbar.panels.version.VersionDebugPanel',
    'debug_toolbar.panels.cache.CacheDebugPanel',
    'debug_toolbar.panels.timer.TimerDebugPanel',
    'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
    'debug_toolbar.panels.headers.HeaderDebugPanel',
    'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
    'debug_toolbar.panels.template.TemplateDebugPanel',
    'debug_toolbar.panels.sql.SQLDebugPanel',
    'debug_toolbar.panels.signals.SignalDebugPanel',
    # 'debug_toolbar.panels.logger.LoggingPanel',
)

def custom_show_toolbar(request):
    return request.user.is_staff

DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS':False,
    'SHOW_TOOLBAR_CALLBACK':custom_show_toolbar,
    'SHOW_TEMPLATE_CONTEXT':True,
    'HIDE_DJANGO_SQL':False,
}

I'm using Django 1.3 with Toolbar version 0.8.5. Any help with this problem would be awesome...

Edit: Based on the answer, I have decided to post how I am handling my view functions:

def func1(query, var1):
    query = query.filter(var__icontains=var1)
    return query

def func2(query, var2):
    query = query.filter(var__icontains=var2)
    return query

def parse(**kwargs):
    # Based on some logic call func1 and func2
    return query

def view(request, template="display.html"):
    # Do some request processing
    query = parse(request.GET.items())
    return render(request, template, { 'items':list(query) })
Candent answered 27/7, 2011 at 4:19 Comment(2)
What version of python? What OS?Stubbs
Python2.7 on a CentOS server. Sorry for the extended hiatus...Candent
D
2

I just find out a way:

  • right click on "default"
  • click inspect element
  • find the nearby table which has style="display:none"
  • edit the style attribute to remove it

I don't know why I have to do all that ...

Dwyer answered 12/9, 2012 at 12:38 Comment(2)
wait, you're saying the SQL is actually there on the page all the while, but hidden?Stubbs
Absolutely ! Ain't that a bummer ?Dwyer
A
6

Make sure that you are running your SQL in the same thread that handled the request.

The Django debug toolbar only seems to take a look at the SQL statements that are run in the current thread and assumes that these are the only ones that are related to the request that was handled.

Araldo answered 27/7, 2011 at 4:24 Comment(6)
Could you show me how I would ensure that? Is there a python module that I can use for this? I am running this on a shared hosting server, and hence a lot of the internals are abstracted from me...Candent
Unless you are explicitly starting python threads inside your code and then running SQL queries from those threads you will be in the same thread that handles the request.Araldo
I have included how some of my view functions work. Would that format cause django to create new threads? Also, the toolbar doesn't seem to be showing any SQL queries in the admin interface. Thanks for all the help and sorry it took me so long to respond...Candent
You said "nothing shows up in the SQL tab" what do you mean by this? Is the tab completely empty, or is there a table that is empty? A screen shot would help. If the tab does not even have an empty table in it then that means that there was an error in the execution of SQLDebugPanel.content or the rendering of the sql.html template, and this would be a bug in the django-debug-toolbar.Araldo
link. I have included a screenshot of the cache tab as well because I don't know if there is an error there as well...Candent
"Make sure that you are running your SQL in the same thread that handled the request.". Look at his screenshot, django-debug-toolbar shows zero SQL request !Dwyer
R
4

I have the same problem, and I found the solution in my case. I am using python 2.5 on Windows Vista. There are 2 problems.

First, the "format" function which is supported from python 2.6 are used in the debug_toolbar.panels.sql module. I fixed this using "%" operator(line 194).

stacktrace.append('<span class="path">%s/</span><span class="file">%s</span> in <span class="func">%s</span>(<span class="lineno">%s</span>)\n <span class="code">%s</span>"' % (params[0], params[1], params[3], params[2], params[4]))

Second, in the same module, '/' character is used as a separation character. Because of this, it does not work on Windows. I changed the separation character and it went well.

Rosecan answered 23/8, 2011 at 10:34 Comment(0)
D
2

I just find out a way:

  • right click on "default"
  • click inspect element
  • find the nearby table which has style="display:none"
  • edit the style attribute to remove it

I don't know why I have to do all that ...

Dwyer answered 12/9, 2012 at 12:38 Comment(2)
wait, you're saying the SQL is actually there on the page all the while, but hidden?Stubbs
Absolutely ! Ain't that a bummer ?Dwyer
R
1

This worked for me:

pip install django-debug-toolbar==0.9.4

Also make sure:

  • DEBUG=True
  • Middleware is after encoders and before Flatpage's

I'm late for few years but there's still people with Django 1.3 around :(

Retrorocket answered 12/8, 2015 at 13:58 Comment(0)
L
0

At the time I'm writing this, it also can happen if you duplicate the debug toolbar middleware. I'm using Django 1.11 with django-debug-toolbar==1.8 .

I had something like this more than once in the code:

if DEBUG and USE_DEBUG_TOOLBAR:
    MIDDLEWARE = ('debug_toolbar.middleware.DebugToolbarMiddleware',) + MIDDLEWARE

See: https://github.com/jazzband/django-debug-toolbar/issues/986

Lucais answered 28/12, 2018 at 4:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.