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

Owner
David Cramer
founder/cto @getsentry
David Cramer
Django friendly finite state machine support

Django friendly finite state machine support django-fsm adds simple declarative state management for django models. If you need parallel task executio

Viewflow 2.1k Dec 31, 2022
This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages.

Source.Python Project Manager This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages. Want to help develop this

2 Sep 24, 2022
A Django application that provides country choices for use with forms, flag icons static files, and a country field for models.

Django Countries A Django application that provides country choices for use with forms, flag icons static files, and a country field for models. Insta

Chris Beaven 1.2k Dec 31, 2022
Sampling profiler for Python programs

py-spy: Sampling profiler for Python programs py-spy is a sampling profiler for Python programs. It lets you visualize what your Python program is spe

Ben Frederickson 9.5k Jan 01, 2023
Pipeline is an asset packaging library for Django.

Pipeline Pipeline is an asset packaging library for Django, providing both CSS and JavaScript concatenation and compression, built-in JavaScript templ

Jazzband 1.4k Jan 03, 2023
An API was build with Django to store and retrieve information about various musical instruments.

The project is meant to be a starting point, an experimentation or a basic example of a way to develop an API with Django. It is an exercise on using Django and various python technologies and design

Kostas Ziovas 2 Dec 25, 2021
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
An insecure login and registration website with Django.

An insecure login and registration website with Django.

Luis QuiƱones Requelme 1 Dec 05, 2021
Simple alternative to Doodle polls and scheduling (Python 3, Django 3, JavaScript)

What is jawanndenn? jawanndenn is a simple web application to schedule meetings and run polls, a libre alternative to Doodle. It is using the followin

Sebastian Pipping 169 Jan 06, 2023
REST API with Django and SQLite3

REST API with Django and SQLite3

Luis QuiƱones Requelme 1 Nov 07, 2021
Simple tagging for django

django-taggit This is a Jazzband project. By contributing you agree to abide by the Contributor Code of Conduct and follow the guidelines. django-tagg

Jazzband 3k Jan 02, 2023
Django Pickled Model

Django Pickled Model Django pickled model provides you a model with dynamic data types. a field can store any value in any type. You can store Integer

Amir 3 Sep 14, 2022
This website serves as an online database (hosted via SQLLite) for fictional businesses in the area to store contact information (name, email, phone number, etc.) for fictional customers.

Django-Online-Business-Database-Project this project is still in progress Overview of Website This website serves as an online database (hosted via SQ

1 Oct 30, 2021
Radically simplified static file serving for Python web apps

WhiteNoise Radically simplified static file serving for Python web apps With a couple of lines of config WhiteNoise allows your web app to serve its o

Dave Evans 2.1k Dec 15, 2022
English dictionary using Django based on freecodecamp

English Dictionary Hi there, i made this english dictionary using Django based on freecodecamp.org tutorial :) Table of Contents Preview Technologies

Aline Alencar 3 May 09, 2022
Django Fett is an incomplete code generator used on several projects

Django Fett Django Fett is an incomplete code generator used on several projects. This is an attempt to clean it up and make it public for consumption

Jeff Triplett 6 Dec 31, 2021
This is a basic Todo Application API using Django Rest Framework

Todo Application This is a basic Todo Application API using Django Rest Framework. Todo Section - User can View his previously added todo items, creat

Atharva Parkhe 1 Aug 09, 2022
Displaying objects on maps in the Django views and administration site.

DjangoAdminGeomap library The free, open-source DjangoAdminGeomap library is designed to display objects on the map in the Django views and admin site

Vitaly Bogomolov 31 Dec 28, 2022
A pickled object field for Django

django-picklefield About django-picklefield provides an implementation of a pickled object field. Such fields can contain any picklable objects. The i

Gintautas Miliauskas 167 Oct 18, 2022
Django datatables with htmx.

Django datatables with htmx.

Regis Santos 7 Oct 23, 2022