#django #python #tools

Debugging a Django app in Visual Studio Code is quite straightforward. The setup just takes a few steps to configure. The only thing you really need to do is to create a debugger launch profile.

Open your project in Visual Studio code and under the "View" menu, select "Run". At the top, you'll see a popup which mentions "No Configurations":

Visual Studio Configurations

When you open the dropdown menu, you can "Add a configuration". You can also click on the gear icon to get to the same menu:

Visual Studio Select Environment

The next popup will ask you to select your environment. In our case, we'll select "Python" obviously. After the environment is selected, Visual Studio Code will allow you to select a specific Python debug configuration. Since we are configuring a Django application, we'll go ahead and select "Django" as the configuration:

Visual Studio Select Debug Configuration

After we did so, Visual Studio Code will add a file called .vscode/launch.json in the root of our project. This file contains a JSON string describing the debug configuration:

 1{
 2    "version": "0.2.0",
 3    "configurations": [
 4        {
 5            "name": "Python: Django",
 6            "type": "python",
 7            "request": "launch",
 8            "program": "${workspaceFolder}/manage.py",
 9            "args": [
10                "runserver"
11            ],
12            "django": true
13        }
14    ]
15}

As you can see, the debugger will launch manage.py with the arguments ["runserver"]. It's also smart enough to known which python interpreter it needs to use to launch the app as that's configured in our project as well. If you are using a Python virtual environment, you need to make sure you have selected the correct python interpreter. If you are not sure, in the bottom toolbar of Visual Studio Code, you can see which intepreter is selected:

Visual Studio See Python Interpreter

If you want to change it, you can click on it and select the correct one (in this case, we are selecting the one from the .venv folder):

Visual Studio Select Python Interpreter

This updates another Visual Studio Code configuration file in your project, .vscode/settings.json to be specific. The setting you alter by changing the python interpreter is python.pythonPath:

 1{
 2    "python.pythonPath": ".venv/bin/python",
 3    "files.exclude": {
 4        "**/.git": true,
 5        "**/.svn": true,
 6        "**/.hg": true,
 7        "**/CVS": true,
 8        "**/__pycache__": true,
 9        "**/.DS_Store": true,
10    }
11}

As you can see in the sample settings.json file, I've also defined files.exclude which lists all files which should be hidden in the Visual Studio file explorer.

To start debugging, you can set one or more breakpoints and run the debugger:

Visual Studio Start Debugging

If you also have other (custom) Django manage commands you wish to debug, you can add them as extra debug configurations. In my blog for example, I have a custom management command called run_cronjobs. If I want to be able to debug that one as well, I can update the launch.json file to:

 1{
 2    "version": "0.2.0",
 3    "configurations": [
 4        {
 5            "name": "Python: Django",
 6            "type": "python",
 7            "request": "launch",
 8            "program": "${workspaceFolder}/manage.py",
 9            "args": [
10                "runserver"
11            ],
12            "django": true
13        },
14        {
15            "name": "Python: Cronjobs",
16            "type": "python",
17            "request": "launch",
18            "program": "${workspaceFolder}/manage.py",
19            "args": [
20                "run_cronjobs"
21            ],
22            "django": true
23        }
24    ]
25}

By doing so, I now how two separate debug configurations I can choose from depending on which command I would like to debug:

Visual Studio Mulitple Debug Configurations

If you want to know more about debugging Django apps in Visual Studio Code, you can check the documentation.