Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add Django debug toolbar to your project?
The Django Debug Toolbar is a powerful debugging tool that displays detailed information about database queries, request/response data, templates, and performance metrics. It's essential for optimizing Django applications during development.
Installation
First, install the django-debug-toolbar package using pip ?
pip install django-debug-toolbar
Configuration Steps
Step 1: Add to INSTALLED_APPS
Add 'debug_toolbar' to your INSTALLED_APPS in settings.py ?
INSTALLED_APPS = [
# ...
'debug_toolbar',
'myapp'
]
Step 2: Configure Middleware
Add the debug toolbar middleware to your MIDDLEWARE setting. It should be placed as early as possible ?
MIDDLEWARE = [
# ...
'debug_toolbar.middleware.DebugToolbarMiddleware',
# ...
]
Step 3: Configure URLs
In your main project's urls.py, add the debug toolbar URL patterns ?
import debug_toolbar
from django.conf import settings
from django.urls import include, path
urlpatterns = [
# ...
path('__debug__/', include(debug_toolbar.urls)),
path('', include('myapp.urls'))
]
Step 4: Set Internal IPs
Add the INTERNAL_IPS setting to specify which IP addresses can see the debug toolbar ?
INTERNAL_IPS = [
'127.0.0.1',
]
Creating a Sample View
Create a simple view to test the debug toolbar. In your app's views.py ?
from django.shortcuts import render
def home(request):
return render(request, "home.html")
Configure the URL pattern in your app's urls.py ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
]
Create a templates folder in your app directory and add home.html ?
<!DOCTYPE html>
<html>
<head>
<title>Django Debug Toolbar Test</title>
</head>
<body>
<h1>Debug Toolbar Demo</h1>
<p>Check the right side of your screen for the debug toolbar.</p>
</body>
</html>
Output
When you run your Django development server and visit your application, you'll see the debug toolbar on the right side of your screen. It provides panels for:
- Versions ? Django and Python version information
- Timer ? Page load time and CPU usage
- SQL ? Database queries with execution time
- Static Files ? Information about CSS, JS files
- Templates ? Template rendering details
- Request ? HTTP request data
Conclusion
The Django Debug Toolbar is an invaluable development tool that helps identify performance bottlenecks and debug issues. Remember to only use it in development environments and never in production for security reasons.
