django-debug-toolbar
repository·main·Indexed 27 days ago
https://github.com/django-commons/django-debug-toolbarA configurable set of panels for Django applications that display real-time debug information about requests, database queries, and performance metrics. Version 0.1.0 includes features such as the debugsqlshell management command for SQL query timing, customizable storage classes (MemoryStore, DatabaseStore, CacheStore), and detailed configuration options for SQL, Profiling, and Templates panels.
What's inside django-debug-toolbar
- The Django Debug Toolbar is a configurable set of panels that display various debug information about the current request/response. When panels are clicked, they display more detailed information about their content. It supports built-in panels as well as third-party community panels.
Use built-in Django Debug Toolbar panels
mainThe Django Debug Toolbar includes several built-in panels to inspect request data, SQL queries, templates, and more. These are enabled by default.
Available Built-in Panels:
HistoryPanel: Shows request history and allows switching to past snapshots.- Note: Disabled if
RENDER_PANELSisTrueor if running with multiple processes.
- Note: Disabled if
VersionsPanel: Shows versions of Python, Django, and installed apps.TimerPanel: Displays the request timer.SettingsPanel: Lists settings fromsettings.py.HeadersPanel: Shows HTTP request/response headers and WSGI environment values.RequestPanel: Displays GET, POST, cookie, and session variables.SQLPanel: Shows SQL queries, execution time, and links toEXPLAINqueries.StaticFilesPanel: Shows used static files and their locations.TemplatesPanel: Shows templates used, context, and template paths.AlertsPanel: Shows alerts (e.g., forms missingenctype="multipart/form-data"when containing file inputs).CachePanel: Shows cache queries (incompatible with Django's per-site caching).SignalsPanel: Lists signals and receivers.CommunityPanel: Provides links to the Django Debug Toolbar community.ProfilingPanel: Provides profiling information for request processing.- Note: Inactive by default. For Python 3.12+, use
python -m manage runserver --nothreading. Concurrent requests are not supported.
- Note: Inactive by default. For Python 3.12+, use
Understand the Django Debug Toolbar architecture
mainThe Django Debug Toolbar is built around three core components that manage integration, orchestration, and data collection:
debug_toolbar.middleware.DebugToolbarMiddleware: The primary integration point. It decides if a request should be instrumented, selects which panels to use, and injects the toolbar's HTML, JavaScript, and headers into the response.debug_toolbar.toolbar.DebugToolbar: The orchestrator. It manages the execution flow across all enabled panels but remains decoupled from the user's specific Django project logic.debug_toolbar.panels: The data collectors. Most complex logic resides here. Panels collect metrics either by inspecting the request/response or via monkey-patching (e.g.,TemplatesPanel). Some panels, likeSQLPanel, include dedicated views (e.g.,debug_toolbar.panels.sql.views) to handle user interactions and display additional data.
Set up a development environment
mainTo work on the Django Debug Toolbar, clone the repository and install the necessary development and documentation dependencies. If you have
fetch.fsckObjectsenabled in your git config, you must deactivate it for this clone to avoid errors with old objects.- Clone the repository (with the specific config if needed):
- Install development and documentation groups using pip.
- Run the example application to verify the setup.
Manually set up the example project
mainIf you prefer to run setup steps individually from the root directory of the repository, use the following commands:
- Create the database:
python example/manage.py migrate - Create a superuser:
python example/manage.py createsuperuser - Run the development server:
python example/manage.py runserver
$ python example/manage.py migrate $ python example/manage.py createsuperuser $ python example/manage.py runserver- Create the database:
Run JavaScript tests
mainThe toolbar includes a JavaScript test suite using
VitestandWebdriverIO. This requires Node.js (checkpackage.jsonfor the supported version) and local installations of Chrome and Firefox.npm install npm testConfigure Django prerequisites for Debug Toolbar
mainEnsure your Django project meets these three requirements:
- Static Files:
'django.contrib.staticfiles'must be inINSTALLED_APPSandSTATIC_URLmust be configured. - Templates: Your
TEMPLATESsetting must use theDjangoTemplatesbackend withAPP_DIRSset toTrue. - Browser: Use a modern browser that meets
Baseline Widely Availablestandards.
INSTALLED_APPS = [ # ... "django.contrib.staticfiles", # ... ] STATIC_URL = "static/" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "APP_DIRS": True, # ... } ]- Static Files:
Disable Debug Toolbar during tests
mainTo prevent the toolbar from running during test suites, wrap the installation logic in a conditional check using aTESTINGflag.Build documentation locally
mainDocumentation is built usingSphinx. You can build it usingtoxto ensure all dependencies are handled automatically, or manually if you have the dependencies installed. For proper spell checking, the Enchant Library must be installed on your system.Install the example project dependencies
mainTo run the sample project, install the required Django and development packages using pip with the
--group devflag.$ python -m pip install --group devUse the debugsqlshell command to inspect database queries
mainThedebugsqlshellcommand starts an interactive Python shell similar to Django's built-inshell. The key difference is that every Django ORM call that results in a database query will automatically print the formatted SQL statement directly to the shell output. This is useful for debugging N+1 problems and verifying thatselect_relatedorprefetch_relatedare working as expected.Configure database permissions for Tox testing
mainIf you are running tests via
toxagainst databases other than SQLite, you must manually create the user and database with appropriate permissions.# For PostgreSQL psql> CREATE USER debug_toolbar WITH PASSWORD 'debug_toolbar'; psql> ALTER USER debug_toolbar CREATEDB; psql> CREATE DATABASE debug_toolbar; psql> GRANT ALL PRIVILEGES ON DATABASE debug_toolbar to debug_toolbar; # For MySQL/MariaDB mysql> CREATE DATABASE debug_toolbar; mysql> CREATE USER 'debug_toolbar'@'localhost' IDENTIFIED BY 'debug_toolbar'; mysql> GRANT ALL PRIVILEGES ON debug_toolbar.* TO 'debug_toolbar'@'localhost'; mysql> GRANT ALL PRIVILEGES ON test_debug_toolbar.* TO 'debug_toolbar'@'localhost';