django-debug-panel icon indicating copy to clipboard operation
django-debug-panel copied to clipboard

Wrong display SQL with django-debug-toolbar 1.4

Open andrey-oparin opened this issue 9 years ago • 9 comments

bug_with_display_sql

andrey-oparin avatar Jan 09 '16 19:01 andrey-oparin

Hi, i upgrade django-debug-toolbar to 1.4, and i get bug with display SQL

andrey-oparin avatar Jan 09 '16 19:01 andrey-oparin

I see the same issue, also with django-debug-toolbar 1.4. @andrey-oparin - did you find any way to fix this?

odedbd avatar Feb 04 '16 14:02 odedbd

same for ddt-1.4 and django 1.9.6

johndark avatar May 12 '16 00:05 johndark

similar in ddt-1.6 and django 1.10.2

eriktelepovsky avatar Oct 20 '16 17:10 eriktelepovsky

I also have this issue with 1.6 and 1.10. Are the developers still around for this project? I've been looking through these GitHub issues and I don't think I've seen a single comment from them?

baylf2000 avatar Nov 01 '16 23:11 baylf2000

Fix here https://github.com/faulkner/django-debug-panel/commit/a8bc8a28d1b9def22632cbdb40a488ce27654f0b , works with Django 1.10.1 and django-debug-toolbar 1.6

chazkii avatar Nov 22 '16 02:11 chazkii

Works perfectly, thank you!

eriktelepovsky avatar Nov 22 '16 09:11 eriktelepovsky

The fix that @chuckus provides doesn't seem to work with AJAX requests (all AJAX requests will show a "No SQL queries" error when viewed through the Chrome extension).

jgbishop avatar Jan 24 '17 19:01 jgbishop

As @jgbishop pointed out, the faulkner patch only fixes the direct requests and the AJAX requests in the panel no longer work. Here's a patch that worked for me:

replace: if hasattr(panel, 'generate_stats'): with: if hasattr(panel, 'generate_stats') and not panel.get_stats():

Essentially, only generate the stats if they haven't been generated already. This seems to prevent the double escaping and probably is more efficient, too.

Since this project seems to be dead, you can use this patch by inheriting and overriding the one method that has the issue and then using it in MIDDLEWARE_CLASSES instead. Here's a patched middleware to use:

"""
This is to patch django-debug-panel to work with latest versions of 
django-debug-toolbar.  The issue is SQL is double-escaped so this
version only calls `generate_stats` on panels if they haven't
already been called.
"""
import threading
import time

import debug_panel
from django.core.urlresolvers import reverse
from debug_panel.cache import cache
from debug_panel.middleware import DebugPanelMiddleware


class DebugPanelMiddlewareFixed(DebugPanelMiddleware):
    def process_response(self, request, response):
        """
        Store the DebugToolbarMiddleware rendered toolbar into a cache store.

        The data stored in the cache are then reachable from an URL that is appened
        to the HTTP response header under the 'X-debug-data-url' key.
        """
        toolbar = self.__class__.debug_toolbars.get(threading.current_thread().ident, None)

        response = super(DebugPanelMiddleware, self).process_response(request, response)

        if toolbar:
            # for django-debug-toolbar >= 1.4
            for panel in reversed(toolbar.enabled_panels):
                if hasattr(panel, 'generate_stats') and not panel.get_stats():  # PATCH HERE
                    panel.generate_stats(request, response)

            cache_key = "%f" % time.time()
            cache.set(cache_key, toolbar.render_toolbar())

            response['X-debug-data-url'] = request.build_absolute_uri(
                reverse('debug_data', urlconf=debug_panel.urls, kwargs={'cache_key': cache_key}))

        return response

tisdall avatar Feb 16 '18 15:02 tisdall