A debug/profiling overlay for Django

Overview

Django Debug Toolbar

The Django Debug Toolbar is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content.

Currently, the following panels have been written and are working:

  • Django version
  • Request timer
  • A list of settings in settings.py
  • Common HTTP headers
  • GET/POST/cookie/session variable display
  • Templates and context used, and their template paths
  • SQL queries including time to execute and links to EXPLAIN each query
  • List of signals, their args and receivers
  • Logging output via Python's built-in logging, or via the logbook module

There is also one Django management command currently:

  • debugsqlshell: Outputs the SQL that gets executed as you work in the Python interactive shell. (See example below)

If you have ideas for other panels please let us know.

Installation

  1. Add the debug_toolbar directory to your Python path.

  2. Add the following middleware to your project's settings.py file:

    'debug_toolbar.middleware.DebugToolbarMiddleware',

    Tying into middleware allows each panel to be instantiated on request and rendering to happen on response.

    The order of MIDDLEWARE_CLASSES is important: the Debug Toolbar middleware must come after any other middleware that encodes the response's content (such as GZipMiddleware).

    Note: The debug toolbar will only display itself if the mimetype of the response is either text/html or application/xhtml+xml and contains a closing </body> tag.

    Note: Be aware of middleware ordering and other middleware that may intercept requests and return responses. Putting the debug toolbar middleware after the Flatpage middleware, for example, means the toolbar will not show up on flatpages.

  3. Make sure your IP is listed in the INTERNAL_IPS setting. If you are working locally this will be:

    INTERNAL_IPS = ('127.0.0.1',)

    Note: This is required because of the built-in requirements of the show_toolbar method. See below for how to define a method to determine your own logic for displaying the toolbar.

  4. Add debug_toolbar to your INSTALLED_APPS setting so Django can find the template files associated with the Debug Toolbar.

    Alternatively, add the path to the debug toolbar templates ('path/to/debug_toolbar/templates' to your TEMPLATE_DIRS setting.)

Configuration

The debug toolbar has two settings that can be set in settings.py:

  1. Optional: Add a tuple called DEBUG_TOOLBAR_PANELS to your settings.py file that specifies the full Python path to the panel that you want included in the Toolbar. This setting looks very much like the MIDDLEWARE_CLASSES setting. For example:

    DEBUG_TOOLBAR_PANELS = (
        'debug_toolbar.panels.version.VersionDebugPanel',
        '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',
    )
    

    You can change the ordering of this tuple to customize the order of the panels you want to display, or add/remove panels. If you have custom panels you can include them in this way -- just provide the full Python path to your panel.

  2. Optional: There are a few configuration options to the debug toolbar that can be placed in a dictionary:

    • INTERCEPT_REDIRECTS: If set to True (default), the debug toolbar will show an intermediate page upon redirect so you can view any debug information prior to redirecting. This page will provide a link to the redirect destination you can follow when ready. If set to False, redirects will proceed as normal.
    • SHOW_TOOLBAR_CALLBACK: If not set or set to None, the debug_toolbar middleware will use its built-in show_toolbar method for determining whether the toolbar should show or not. The default checks are that DEBUG must be set to True and the IP of the request must be in INTERNAL_IPS. You can provide your own method for displaying the toolbar which contains your custom logic. This method should return True or False.
    • EXTRA_SIGNALS: An array of custom signals that might be in your project, defined as the python path to the signal.
    • HIDE_DJANGO_SQL: If set to True (the default) then code in Django itself won't be shown in SQL stacktraces.
    • SHOW_TEMPLATE_CONTEXT: If set to True (the default) then a template's context will be included with it in the Template debug panel. Turning this off is useful when you have large template contexts, or you have template contexts with lazy datastructures that you don't want to be evaluated.
    • TAG: If set, this will be the tag to which debug_toolbar will attach the debug toolbar. Defaults to 'body'.

    Example configuration:

    def custom_show_toolbar(request):
        return True # Always show toolbar, for example purposes only.
    
    DEBUG_TOOLBAR_CONFIG = {
        'INTERCEPT_REDIRECTS': False,
        'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
        'EXTRA_SIGNALS': ['myproject.signals.MySignal'],
        'HIDE_DJANGO_SQL': False,
        'TAG': 'div',
    }
    

debugsqlshell

The following is sample output from running the debugsqlshell management command. Each ORM call that results in a database query will be beautifully output in the shell:

$ ./manage.py debugsqlshell
Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from page.models import Page
>>> ### Lookup and use resulting in an extra query...
>>> p = Page.objects.get(pk=1)
SELECT "page_page"."id",
       "page_page"."number",
       "page_page"."template_id",
       "page_page"."description"
FROM "page_page"
WHERE "page_page"."id" = 1

>>> print p.template.name
SELECT "page_template"."id",
       "page_template"."name",
       "page_template"."description"
FROM "page_template"
WHERE "page_template"."id" = 1

Home
>>> ### Using select_related to avoid 2nd database call...
>>> p = Page.objects.select_related('template').get(pk=1)
SELECT "page_page"."id",
       "page_page"."number",
       "page_page"."template_id",
       "page_page"."description",
       "page_template"."id",
       "page_template"."name",
       "page_template"."description"
FROM "page_page"
INNER JOIN "page_template" ON ("page_page"."template_id" = "page_template"."id")
WHERE "page_page"."id" = 1

>>> print p.template.name
Home

TODOs and BUGS

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

Comments
  • multiple jquery installations

    multiple jquery installations

    I was having lots of issues with jQuery and I just realized that it was because debug-toolbar is adding their own version of the library.

    It was somehow resetting my plugins when your jQuery library was pulled. I see that you are running it in no conflict, but unfortunately that is doing me no good. I was able to comment out the inclusion of your library (v 1.2.6) and just let debug-toolbar use mine (v 1.3.2) and everything worked fine.

    It would be nice if debug-toolbar could recognize if jQuery was already installed and then just skip it's own installation.

    opened by aroscoe 5
  • Using raw sql (cursors) with executemany breaks debug toolbar

    Using raw sql (cursors) with executemany breaks debug toolbar

    Relevant traceback:

    File 'E:\Projects\farmforce\farmforce\fftenant\tags.py', line 77 in add_multiple_tags cursor.executemany(query, params) File 'C:\Python27\lib\site-packages\django_debug_toolbar-0.8.5-py2.7.egg\debug_toolbar\panels\sql.py', line 199 in executemany 'sql': self.db.ops.last_executed_query(self.cursor, sql, params), File 'C:\Python27\lib\site-packages\django\db\backends*init*.py', line 505 in last_executed_query return smart_unicode(sql) % u_params TypeError: not all arguments converted during string formatting

    opened by eloff 3
  • Profiling and debugging a production site

    Profiling and debugging a production site

    Currently, the way DDT works is by configuring the deployment in DEBUG mode (DEBUG = True), and by specifying the IP addresses of those that should have access. This all works quite nicely in a development environment, but it makes it extremely difficult to profile a live site.

    Has there been discussion of possible alternative methods for enabling the toolbar on a live site? Perhaps by using permissions, or simply the user.is_admin()? If developers are always able to access debug information, chances are higher that performance will be monitored via developers dogfooding their own sites.

    I'd be quite interested in developing a different method for enabling the toolbar. I'm just wondering if I'm covering old ground, and if there are significant reasons that things work the way they do?

    opened by jarshwah 2
  • SQL panel makes error in Django 1.3

    SQL panel makes error in Django 1.3

    In Django 1.3, debug_toolbar.panels.sql.SQLDebugPanel makes an error: TemplateSyntaxError at / Caught AttributeError while rendering: 'Cursor' object has no attribute 'db'

    opened by ghost 1
  • rev 15493 breaks debug_toolbar

    rev 15493 breaks debug_toolbar

    filling a new ticket as I accidentally closed the last one and can't open it again. CursorWrapper.db was renamed to CursorWrapper.connection , this should be fixed before 1.3 lands..

    https://github.com/robhudson/django-debug-toolbar/blob/master/debug_toolbar/panels/sql.py#L117

    Filled under issue 139 in rob's fork

    opened by rasca 1
  • settings.MEDIA_URL could be None

    settings.MEDIA_URL could be None

    Hi,

    Here is a quick bug fix. settings.MEDIA_URL is used when deciding if the toolbar should be visible or not. But in my setup settings.MEDIA_URL is None (I use django-static for static files) so the toolbar crashes.

    ///Kalle

    opened by kalle 1
  • Profiling panel: Caught ValueError while rendering: need more than 1 value to unpack

    Profiling panel: Caught ValueError while rendering: need more than 1 value to unpack

    Same as Issue #16, but for profiling panel.

    On Windows, splitting a path with '/' does not work well ;-)

    The exception is located at debug_toolbar/panels/profiling.py in content, line 65 Bug fixed by importing os, and replacing '/' by os.path.sep

    opened by olexiystrashko 0
  • Django gives errors when DjDT tries to render itself on JSON responses.

    Django gives errors when DjDT tries to render itself on JSON responses.

    Perhaps DjDT needs to check the response MIME type and only insert itself on JSON responses. We're using a RestView class (http://djangosnippets.org/snippets/1071/) with the django-annoying @ajax_request decorator and DjDT tries to attach itself to the response, failing.

    The traceback:

    Traceback (most recent call last):
      File "/var/myproject/env/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
        return self.application(environ, start_response)
      File "/var/myproject/env/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 273, in __call__
        response = self.get_response(request)
      File "/var/myproject/env/lib/python2.6/site-packages/django/core/handlers/base.py", line 182, in get_response
        response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
      File "/var/myproject/env/lib/python2.6/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
        return debug.technical_500_response(request, *exc_info)
      File "/var/myproject/env/lib/python2.6/site-packages/django/core/handlers/base.py", line 178, in get_response
        response = middleware_method(request, response)
      File "/var/myproject/env/lib/python2.6/site-packages/debug_toolbar/middleware.py", line 106, in process_response
        smart_unicode(self.debug_toolbars[request].render_toolbar() + self.tag))
      File "/var/myproject/env/lib/python2.6/site-packages/debug_toolbar/toolbar/loader.py", line 78, in render_toolbar
        return render_to_string('debug_toolbar/base.html', context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/loader.py", line 183, in render_to_string
        return t.render(Context(dictionary))
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/base.py", line 123, in render
        return self._render(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/test/utils.py", line 57, in instrumented_test_render
        return self.nodelist.render(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/base.py", line 744, in render
        bits.append(self.render_node(node, context))
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
        result = node.render(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/defaulttags.py", line 227, in render
        nodelist.append(node.render(context))
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/defaulttags.py", line 311, in render
        return self.nodelist_true.render(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/base.py", line 744, in render
        bits.append(self.render_node(node, context))
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
        result = node.render(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/debug.py", line 90, in render
        output = self.filter_expression.resolve(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/base.py", line 510, in resolve
        obj = self.var.resolve(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/base.py", line 653, in resolve
        value = self._resolve_lookup(context)
      File "/var/myproject/env/lib/python2.6/site-packages/django/template/base.py", line 698, in _resolve_lookup
        current = current()
      File "/var/myproject/env/lib/python2.6/site-packages/debug_toolbar/panels/request_vars.py", line 35, in content
        'view_func': '%s.%s' % (self.view_func.__module__, self.view_func.__name__),
    TemplateSyntaxError: Caught AttributeError while rendering: 'RestView' object has no attribute '__name__'
    
    opened by skorokithakis 1
  • Caught IndexError while rendering: tuple index out of range

    Caught IndexError while rendering: tuple index out of range

    On Windows, splitting a path with '/' does not work well ;-)

    The exception is located at debug_toolbar/panels/sql.py in content, line 195 Bug fixed by importing os, and replacing '/' by os.path.sep

    opened by tlesne 4
Owner
David Cramer
founder/cto @getsentry
David Cramer
Meta package to combine turbo-django and stimulus-django

Hotwire + Django This repository aims to help you integrate Hotwire with Django ?? Inspiration might be taken from @hotwired/hotwire-rails. We are sti

Hotwire for Django 31 Aug 9, 2022
django-reversion is an extension to the Django web framework that provides version control for model instances.

django-reversion django-reversion is an extension to the Django web framework that provides version control for model instances. Requirements Python 3

Dave Hall 2.8k Jan 2, 2023
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Daniele Faraglia 2.7k Jan 7, 2023
Rosetta is a Django application that eases the translation process of your Django projects

Rosetta Rosetta is a Django application that facilitates the translation process of your Django projects. Because it doesn't export any models, Rosett

Marco Bonetti 909 Dec 26, 2022
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.

Cookiecutter Django Powered by Cookiecutter, Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly. Documentati

Daniel Feldroy 10k Dec 31, 2022
Django project starter on steroids: quickly create a Django app AND generate source code for data models + REST/GraphQL APIs (the generated code is auto-linted and has 100% test coverage).

Create Django App ?? We're a Django project starter on steroids! One-line command to create a Django app with all the dependencies auto-installed AND

imagine.ai 68 Oct 19, 2022
django-quill-editor makes Quill.js easy to use on Django Forms and admin sites

django-quill-editor django-quill-editor makes Quill.js easy to use on Django Forms and admin sites No configuration required for static files! The ent

lhy 139 Dec 5, 2022
A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, celery and redis.

Django Channels Websocket Chatbot A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, c

Yunbo Shi 8 Oct 28, 2022
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

sageteam 51 Sep 15, 2022
A beginner django project and also my first Django project which involves shortening of a longer URL into a short one using a unique id.

Django-URL-Shortener A beginner django project and also my first Django project which involves shortening of a longer URL into a short one using a uni

Rohini Rao 3 Aug 8, 2021
Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot. A fully Django starter project.

Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot ?? Features A Django stater project with fully basic requirements for a production-ready

null 8 Jun 27, 2022
pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-dev 1.1k Dec 14, 2022
APIs for a Chat app. Written with Django Rest framework and Django channels.

ChatAPI APIs for a Chat app. Written with Django Rest framework and Django channels. The documentation for the http end points can be found here This

Victor Aderibigbe 18 Sep 9, 2022
django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing

django-dashing django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project.

talPor Solutions 703 Dec 22, 2022
Django-MySQL extends Django's built-in MySQL and MariaDB support their specific features not available on other databases.

Django-MySQL The dolphin-pony - proof that cute + cute = double cute. Django-MySQL extends Django's built-in MySQL and MariaDB support their specific

Adam Johnson 504 Jan 4, 2023
Django-Audiofield is a simple app that allows Audio files upload, management and conversion to different audio format (mp3, wav & ogg), which also makes it easy to play audio files into your Django application.

Django-Audiofield Description: Django Audio Management Tools Maintainer: Areski Contributors: list of contributors Django-Audiofield is a simple app t

Areski Belaid 167 Nov 10, 2022
django Filer is a file management application for django that makes handling of files and images a breeze.

django Filer is a file management application for django that makes handling of files and images a breeze.

django CMS Association 1.6k Jan 6, 2023
Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

tzangms 557 Oct 19, 2022
Blog focused on skills enhancement and knowledge sharing. Tech Stack's: Vue.js, Django and Django-Ninja

Blog focused on skills enhancement and knowledge sharing. Tech Stack's: Vue.js, Django and Django-Ninja

Wanderson Fontes 2 Sep 21, 2022