Naveera A.
—What is the best way to debug a Django application? Are there any Django-specific tools to help you debug in Django?
For example, how can you debug why a template is taking too long to render? Or why an API endpoint is crashing? Is there a better way to debug than using print
statements?
There are many tools available to help us debug a Django application. Here are a few of the more popular ones:
Django shell is a Python shell that lets us access the database API included with Django. When we open the Django shell, Django loads all dependencies for the project and imports Django settings, allowing us to evaluate expressions related to our project.
We can start the Django shell using the ‘manage.py’ file, like so:
$ python manage.py shell
The Django Debug Toolbar is a visual tool that helps us debug the Django application.
The Django debug toolbar offers information on every page of our app using a sliding sidebar. It gives us information about various parts of the app like the current request/response, resource usage (e.g. time), Django settings, HTTP headers, SQL queries, cache, logging, and more.
We can install the toolbar with pip
:
(venv) $ pip install django-debug-toolbar
Now we need to add it to the project’s INSTALLED_APPS
:
# settings.py INSTALLED_APPS = [ 'my_project', ... 'debug_toolbar' ]
Then we will need to add it to the application’s MIDDLEWARE_CLASSES
:
# settings.py MIDDLEWARE_CLASSES = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', ... 'debug_toolbar.middleware.DebugToolbarMiddleware' ]
The toolbar’s sidebar will appear on any connection that matches Django’s INTERNAL_IPS
if the DEBUG
value is set to True
.
Python’s built-in debugging module pdb
is an excellent tool to debug any Python application interactively.
Django PDB is a package that allows us to use the pdb
module in the context of Django applications. It automatically activates the pdb
for any endpoint.
We can install the django-pdb
module using pip
:
(venv) $ pip install django-pdb
Next we need to add it to the end of the application’s INSTALLED_APPS
:
# settings.py INSTALLED_APPS = [ 'my_project', ... 'django_pdb' ]
Then we will need to add it to the end of the application’s MIDDLEWARE_CLASSES
:
# settings.py MIDDLEWARE_CLASSES = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', ... 'django_pdb.middleware.PdbMiddleware' ]
Django PDB is fairly easy to use if you are familiar with Python’s pdb
module. You can learn more about its usage in the documentation.
Lastly, we can also use the built-in debugging features of the IDE or code editor to debug a Django application.
Here are some links to debug a Django app in different IDEs and code editors:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.