Django: 4 steps to add “django-debug-toolbar”
2 min readSep 26, 2024
This tool will help to investigate ORM query command in Django, so let’s get to the point on how to install in Djang project.
- Install lib of
django-debug-toolbar~=3.2.4
in project.
- Add
django-debug-toolbar~=3.2.4
inrequirements.in
- Run this docker command (to compile .in file) or if not use docker can add
django-debug-toolbar==3.2.4
to requiremenst.txt directly.
docker-compose exec django sh -c "pip install pip-tools && pip-compile"
2. Rebuild Docker image again like this command.
docker-compose build django
3. Add app in settings.py
INSTALLED_APPS += [
'debug_toolbar',
]
MIDDLEWARE += [
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': lambda _request: DEBUG
}
4. Add config this 2 lines in urls.py
import debug_toolbar
path('__debug__/', include(debug_toolbar.urls)),
import debug_toolbar
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
if settings.DEBUG or TESTING:
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
] + static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT
) + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
5. Done and now can show Django debug toolbar
in Browsable API.
Ref:
https://www.youtube.com/watch?v=H-vLUoXKKIs