Adding Django Debug Toolbar: A Step-by-Step Guide

Abdullah Sohail
4 min readJun 26, 2023

--

Introduction:

In the world of web development, debugging is an essential part of ensuring the smooth operation of your Django applications. Django Debug Toolbar is a powerful tool that provides valuable insights into your application’s performance and helps identify and fix issues effectively. In this tutorial, we will walk you through the process of adding Django Debug Toolbar to your Django project using Visual Studio Code’s integrated terminal.

Prerequisites:

Before we dive into the process, make sure you have the following prerequisites in place:

  1. Visual Studio Code: If you haven’t already installed Visual Studio Code, you can download it from the official website (https://code.visualstudio.com/).
  2. Django Project: Set up a Django project or have an existing project that you would like to add the debug toolbar to (https://medium.com/@abdullahsohailcs/setting-up-your-development-environment-and-creating-your-first-django-project-4ec7d4e758a6).

Let’s get started!

Step 1: Opening the Integrated Terminal in Visual Studio Code To begin, open Visual Studio Code and navigate to the terminal. If you’re unfamiliar with this process, you can refer to this guide (https://medium.com/@abdullahsohailcs/how-to-run-django-on-visual-studio-code-using-the-integrated-terminal-using-the-integrated-e73bbe80cd91) for detailed instructions.

Step 2: Activating the Virtual Environment Once you have the terminal open in Visual Studio Code, create a new terminal and execute the following command to activate the virtual environment using Pipenv:

pipenv shell

Step 3: Installing Django Debug Toolbar With the virtual environment activated, we can now install the Django Debug Toolbar using the following command:

python -m pip install django-debug-toolbar

Step 4: Configuring the Django Project Now that Django Debug Toolbar is installed, we need to configure our Django project to use it.

4.1. Open your project’s settings.py file.

4.2. Locate the INSTALLED_APPS list and add 'debug_toolbar' to the list:

INSTALLED_APPS = [
# ...
'debug_toolbar',
# ...
]

4.3. Open your project’s urls.py file.

4.4. Import the debug_toolbar module at the top:

import debug_toolbar

4.5. Add the following path to your urlpatterns list, placing it at index 0:

urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
# ...
]

4.6. Return to your settings.py file.

4.7. Locate the MIDDLEWARE setting and add 'debug_toolbar.middleware.DebugToolbarMiddleware' to the list:

MIDDLEWARE = [
# ...
'debug_toolbar.middleware.DebugToolbarMiddleware',
# ...
]

4.8. Additionally, add the following line anywhere in the settings.py file to specify the internal IP addresses:

INTERNAL_IPS = [
# ...
'127.0.0.1',
# ...
]

Step 5: Running the Project without Debugging With the Django Debug Toolbar integrated into your project, you can now run the project without enabling the toolbar. In Visual Studio Code, use the appropriate command or shortcut to start your Django project as you typically would, without the debug mode.

Conclusion:

Congratulations! You have successfully added Django Debug Toolbar to your Django project using Visual Studio Code’s integrated terminal. By following these steps, you now have a powerful tool at your disposal for debugging and analyzing your Django applications. The Django Debug Toolbar will assist you in identifying and resolving any performance-related issues, ultimately leading to an improved user experience.

Remember, the Django Debug Toolbar is a valuable asset during development, but it’s important to disable it when deploying your application to a production environment. Always ensure that the toolbar is only active in your development environment.

To run your Django project without the debug toolbar through Visual Studio Code, follow these steps:

  • At the top menu of Visual Studio Code, locate and click on the “Run” menu option.
  • From the dropdown menu that appears, select “Run Without Debugging”.

Please note that it’s important to ensure that your virtual environment is activated and that you are in the correct working directory (the root directory of your Django project) before executing these steps.

I hope this clarifies the instructions for running your Django project without debugging in Visual Studio Code. If you have any further questions, feel free to ask!

--

--