Skip to main content

Introduction

The debugtools module offers some easy to use debugging utilities to assist Django development. It features:

  • A template tag to print context.

  • A XViewMiddleware variation to see which view and template was used to render a page.

  • A panel for django-debug-toolbar to show which view and template was used to render a page.

  • A jQuery debug() function.

Installation

First install the module, preferably in a virtual environment. It can be installed from PyPI:

pip install django-debugtools

Or the current folder can be installed:

pip install .

Configuration

Add the module to the installed apps:

INSTALLED_APPS += (
    'debugtools',
)

As of Django 1.9, either use {% load debugtools_tags %} or add the following to the settings:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                # ...
            ],
           'builtins': [                                     # Add this section
                "debugtools.templatetags.debugtools_tags",   # Add this line
            ],
        },
    },
]

Or, when you use a local.py settings file:

TEMPLATES[0]['OPTIONS']['builtins'] += [
    "debugtools.templatetags.debugtools_tags",  # enables {% print %}
]

Features

Print Template Tag

In Django templates, the following code can be used:

{% print variable1 variable2 %}

This will print out the specific variables, in case of {% print original %}:

https://github.com/edoburu/django-debugtools/raw/master/docs/images/print-original.png

When no variables are given (e.g. {% print %}), all context variables are displayed:

https://github.com/edoburu/django-debugtools/raw/master/docs/images/template-context.png

The template context variables are printed in a customized pprint.pformat format, for easy reading. Note no {% load %} tag is needed; the {% print %} function is added to the template builtins for debugging convenience.

Print Queries template tag

For convenience, there is also a {% print_queries %} tag, based on http://djangosnippets.org/snippets/93/

For more sophisticated debugging, you may want to use the django-debug-toolbar for this job.

Debug Toolbar Panel

Add the following settings to your django-debug-toolbar configuration:

DEBUG_TOOLBAR_PANELS = (
    'debug_toolbar.panels.versions.VersionsPanel',
    'debug_toolbar.panels.timer.TimerPanel',
    'debug_toolbar.panels.settings.SettingsPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    'debugtools.panels.ViewPanel',    # Add this one
    'debug_toolbar.panels.templates.TemplatesPanel',
    'debug_toolbar.panels.cache.CachePanel',
    'debug_toolbar.panels.signals.SignalsPanel',
    'debug_toolbar.panels.logging.LoggingPanel',
    'debug_toolbar.panels.redirects.RedirectsPanel',
)
https://github.com/edoburu/django-debugtools/raw/master/docs/images/debug-toolbar.png

jQuery debug print

Add the following to the page:

<script type="text/javascript" src="{{ STATIC_URL }}debugtools/jquery.debug.js"></script>

Now you can print the jQuery selector context to the console:

$("#foo").children('li').debug().addClass('bar');

This will print the matched <li> elements in the console, among with the current jQuery selector. Optionally, a prefix can be included in the debug() call:

$("#foo").debug("at baz: ").addClass('bar');

X-View Middleware

As alternative to the django-debug-toolbar panel, you can also add the XViewMiddleware. Add the following setting:

INTERNAL_IPS = (
    '127.0.0.1',
)

MIDDLEWARE_CLASSES += (
    'debugtools.middleware.XViewMiddleware',
)

All requests from the internal IP, or made by the admin user will have a X-View header and X-View-Template header. In the Firebug console, or Chrome web inspector, you can see which view and template handled the current request:

https://github.com/edoburu/django-debugtools/raw/master/docs/images/firebug-xview.png

The alternative templates are also displayed, in case the view allows the template to be overwritten with a different name.

Print tag examples

For example, when using the following code:

{% print original %}

{% print inline_admin_formset %}

{% for inline_admin_form in inline_admin_formset %}
    {% print inline_admin_form %}
    {% print inline_admin_form.form.name %}
{% endfor %}

It prints the context values, which helps to learn a lot about the template context:

https://github.com/edoburu/django-debugtools/raw/master/docs/images/print-original.png

https://github.com/edoburu/django-debugtools/raw/master/docs/images/inline_admin_formset.png

https://github.com/edoburu/django-debugtools/raw/master/docs/images/inline_admin_form.png

https://github.com/edoburu/django-debugtools/raw/master/docs/images/adminform.form.name.png

This makes it much easier to understand what the code provides to templates.

Release files for django-debugtools 2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-debugtools 2.0
File Size Uploaded
django-debugtools-2.0.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-debugtools 2.0
File Interpreter ABI Platform
django_debugtools-2.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.1 kB

Release files / django-debugtools-2.0.tar.gz

Download URL django-debugtools-2.0.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
085f7bc62a3a5c81c7a0d8643dbc7dfd991cfdffabe7a6d6fbecad0e346608ff
BLAKE2b-256 checksum
How to use checksums
46f1e25cc343eec590a1e0f77619dc004b08641fde29459e64f00d6f01b0f2ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

Release files / django_debugtools-2.0-py3-none-any.whl

Download URL django_debugtools-2.0-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9b3de4ee0aa1b2b9290bf8a09e1cd52baacfc7a8214abe15e8bfb80f2c2f497
BLAKE2b-256 checksum
How to use checksums
83b5fc4093f07cd87c5c6efda16b244df79bc91e2d6a201d3e293bf9f369d677
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

Release history Release notifications | RSS feed

This release

2.0 This release

2 release files

1.8

2 release files

1.7.4

2 release files

1.7.3

2 release files

1.7.2

2 release files

1.7.1

2 release files

1.7

2 release files

1.6

2 release files

1.5.1

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2.1

2 release files

1.2

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.9.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page