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:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true
        }
    ]
}

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:

{
    "python.pythonPath": ".venv/bin/python",
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/__pycache__": true,
        "**/.DS_Store": true,
    }
}

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:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true
        },
        {
            "name": "Python: Cronjobs",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "run_cronjobs"
            ],
            "django": true
        }
    ]
}

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.

Related Posts

  • Migrating your Wagtail site to a different database engine
  • Making publish the default action in Wagtail
  • Outputting a Django queryset as JSON
  • Programatically creating redirects in Wagtail
  • Generating slugs and redirects with Django