Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Debug Toolbar not displaying SQL

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) })
like image 670
banerjs Avatar asked Jul 27 '11 04:07

banerjs


People also ask

Why does Django not show Debug Toolbar?

The Debug Toolbar is shown only if your IP address is listed in Django's INTERNAL_IPS setting. This means that for local development, you must add "127.0. 0.1" to INTERNAL_IPS .

How does Django Debug Toolbar work?

The Debug Toolbar works in two phases. First, it gathers data while Django handles a request and stores this data in memory. Second, when you open a panel in the browser, it fetches the data on the server and displays it.

Does Django Debug Toolbar work with DRF?

Yes, Debug Toolbar works with DRF, but you need also to add INTERNAL_IPS = ['127.0. 0.1',] to your settings.py file. Make sure you put the right ip-address to INTERNAL_IPS if you use a reverse proxy.


3 Answers

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.

like image 200
Alex Q Avatar answered Oct 04 '22 17:10

Alex Q


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.

like image 40
adam japan Avatar answered Oct 04 '22 16:10

adam japan


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 ...

like image 32
jpic Avatar answered Oct 04 '22 15:10

jpic