Django Debug Toolbar is an essential tool for monitoring and optimizing Django applications. It provides insights into SQL queries, cache usage, template rendering time, and more.
Install the package using pip:
pip install django-debug-toolbar
Modify settings.py
to include 'debug_toolbar'
in INSTALLED_APPS
:
INSTALLED_APPS = [
...
"debug_toolbar",
]
Insert 'debug_toolbar.middleware.DebugToolbarMiddleware'
before 'django.middleware.common.CommonMiddleware'
in MIDDLEWARE
:
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.common.CommonMiddleware",
...
]
Django Debug Toolbar is only available for internal IPs. Add this setting to settings.py
:
INTERNAL_IPS = [
"127.0.0.1",
]
If using Docker, use:
import socket
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + "1" for ip in ips]
Modify urls.py
to include Debug Toolbar’s URLs:
from django.conf import settings
from django.conf.urls import include
from django.urls import path
urlpatterns = [
...
]
if settings.DEBUG:
import debug_toolbar
urlpatterns += [path("__debug__/", include(debug_toolbar.urls))]
Start the development server:
python manage.py runserver
Visit your project in the browser, and you should see the Django Debug Toolbar appear on the right side of the screen.
The toolbar provides several built-in panels for debugging:
Customize which panels appear in settings.py
:
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",
"debug_toolbar.panels.templates.TemplatesPanel",
"debug_toolbar.panels.cache.CachePanel",
"debug_toolbar.panels.signals.SignalsPanel",
"debug_toolbar.panels.logging.LoggingPanel",
]
To log all SQL queries in real time, enable query logging:
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": lambda request: True,
"SQL_WARNING_THRESHOLD": 100, # Highlight queries taking more than 100ms
}
You can also enable detailed query execution plans with:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("EXPLAIN ANALYZE SELECT * FROM my_table")
print(cursor.fetchall())
Ensure Debug Toolbar is active only in development:
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": lambda request: request.META.get("REMOTE_ADDR") in INTERNAL_IPS,
}
If caching is enabled, Debug Toolbar provides insights into cache performance.
from django.core.cache import cache
cache.set("my_key", "my_value", timeout=60)
value = cache.get("my_key")
print(value) # Output: my_value
The toolbar shows:
{% endraw %}{% for book in books %}
<p></p>
{% endraw %}{% endfor %}
With Debug Toolbar, you can check if template rendering is slow due to large context data.
Debug Toolbar provides HTTP request and response headers, useful for debugging:
def my_view(request):
print(request.headers)
return HttpResponse("Check Debug Toolbar for headers")
Never use Django Debug Toolbar in production. Disable it using:
if not DEBUG:
INSTALLED_APPS.remove("debug_toolbar")
MIDDLEWARE.remove("debug_toolbar.middleware.DebugToolbarMiddleware")
Or completely disable it with:
SHOW_TOOLBAR_CALLBACK = lambda request: False
Feature | Benefit |
---|---|
SQL Queries Panel | Shows all executed queries and highlights slow ones |
Templates Panel | Displays template load times and context variables |
Cache Panel | Shows cache hits, misses, and usage |
Static Files Panel | Monitors loaded static files |
Headers Panel | Displays HTTP request/response headers |
Signals Panel | Shows all Django signals being triggered |
Logging Panel | Displays log messages in real-time |