How To Debug in Django. Useful Tips

Many people think that throughout our professional career, developers spend most of their time troubleshooting bugs in applications that we implement or reverse engineer, so one of our biggest allies should be debuggers. From BeDjango we will discuss a series of methods and utilities, some very simple and others more complex, to help you in your Django projects, although some of the following tools can be used without problemseasily in any Python project.


The first and perhaps the best known tool, is to configure a good logging system in our application, which allows us to detect errors and even register actions that we want to monitor. Django is based on the package logging of the standard Python library and the configuration is very simple, permitting a default logging in our Django projects. In any case, if you want to configure a logging for the first time you want to do so and we encourage you to do so. Out of interest here are the different levels of messages that we can control:

  • DEBUG: Low level system information for debugging purposes.

  • INFO: General system information.

  • WARNING: Information describing a minor problem that has occurred.

  • ERROR: Information describing a major problem that has occurred.

  • CRITICAL: Information describing a critical problem that has occurred.

Django Debug toolbar

Another of the tools that we use most in our daily lives is a Django application that allows us to monitor our project. The Django Debug Toolbar is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content. In these panels you can check things like logging, requests to the database, values of variables, headers... If you want to configure it in your project, go to our post about its installationor if you want to try it easily and in just few minutes you could try out our starter

Image url:

Django Developer Panel

This tool is an alternative to the previous panel, since in a way it has most of its features. To do this we will need to install two packages, the first of which is a Django application called Django Developer Panel whose configuration in our project is very simple, as its documentation indicates:

First you must install it using pip:

pip install django-developer-panel

Later Add the Developer Panel middleware to your application's and make sure DEBUG is enabled:

DEBUG = True

   'djdev_panel.middleware.DebugMiddleware',  # <--- this guy

Next, we must install a plugin for the Google Chrome browser that will allow us to use DevTools to control different parameters such as variable values, session variables, details of views, user data...

How to Debug templates

Although the following utility is not a tool, it is an aid that some developers do not know or have never needed to use. There is a template called debug, which we can use in our templates to show the value of all variables, to use it we must have the DEBUG option of settings. py to True and it is used by writing the following code in our templates:

{% debug %}

pdb & ipdb & pudb

1. Pdb

Finally we will comment on three debuggers for Python code (so you can use them for any kind of Python based development), for us the unfolding with some of these tools is considered a MUST HAVE, since they will not only help you to find and fix bugs, but you can also use them as an aid to programming, since they allow you to stop anywhere in the code running and for example overwrite values of these three packages allow us, like any other debugger, to print the value of the variables, evaluate the source code line by line and inspect the operation of the functions and methods.

First of all we have the pdb package of the standard Python library, it will allow us to stop the execution of the code in the place we want, for it first import the package:

import pdb

and where we want to stop it:


Once the execution is stopped, we can print variables by typing them, assign new values to modify the execution... Some of the commands that we use are the following:

  1. w(here): Print a stack trace, with the most recent frame at the bottom.
  2. s(tep):Execute the current line, stop at the first possible occasion
  3. n(ext):Continue execution until the next line in the current function is reached or it returns.
  4. r(eturn): Continue execution until the current function returns.
  5. c(ont(inue)):Continue execution, only stop when a breakpoint is encountered.
  6. l(ist): List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines starting at that line. With two arguments, list the given range; if the second argument is less than the first, it is a count.

A good cheatsheet if you want to increase your knowledge can be obtained here.

2. ipdb

The following list is called ipdb, it is a third party package that in addition to all the pdb functionalities adds integration with iPython (tab completion, object introspection, friendlier tracebacks, color support...) the use is identical to the previous one. To install it we will use the following command:

pip install ipdb

and to indicate where in our code we want to stop using it:

import ipdb; idpb.set_trace()

The commands are similar to those mentioned above, you can get more information in the following cheatsheet

3. pudb

Finally we have PuDB , which is a full-screen, console-based visual debugger for Python. This interface has different panels, the left panel shows the code where the execution is stopped, while to the right we have different panels where we can see the value of the variables, the breakpoints that we have activated...The interface is quite configurable, from displaying the number of lines, the color we want to use... The commands are similar to pdb, with the difference that you will have to move around the different panels, but the help texts of the interface will help us with the configuration in a simple way to handle quickly the different panels. Another advantage is that thanks to the next package, we can integrate pudb with iPython. To install it we will use:

pip install pudb

and to indicate where in our code we want to stop using:

from pudb import set_trace; set_trace()

or the abbreviated version:

import pudb; pu.db

Image url

I hope you liked the content of this post and that it is useful to you! If you have any questions or suggestions do not hesitate to contact me on social networks or send me an email at

About the author

Let’s have a coffee and talk about your project


Let’s have a coffee and talk about your project


We use cookies to ensure you get the best experience on our website. More info.