[Fixed]-How to debug Django custom management command using VS Code

18👍

Rik’s answer is correct, but requires changing the launch config for every management command, or creating multiple launch config entries.

To create one entry that can debug all management commands, even the ones you still have to write, add the following config to your launch.json:

        {
            "name": "Django MGMT Command",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "${fileBasenameNoExtension}"
            ]
        }

This works because the name of the management command is the name of the file in which it’s defined, without the .py extension. Or ${fileBasenameNoExtension} for short.

See https://code.visualstudio.com/docs/editor/variables-reference for other variables you can use in your launch config.

👤jrial

20👍

While writing this question I came up with a solution myself.

In the VS Code launch.json file (which contains the settings for the VS Code Django debugger) contains the following entry, by default:

"args": ["runserver", "--noreload", "--nothreading"]

I changed this to:

"args": ["name_of_management_command"]

Then start the debugger (press f5), and I am debugging my custom management command

3👍

@Rik and @jrial are missing an important argument, which is justMyCode, suppose you want to do a custom command that use startprojectapp and you want to see the code behind it to understand how to pass the arguments. You need to specify that argument for being able to enter there.
Also, I prefer to select the custom command from a list, instead of having to create one configuration to each file (@Rik solution), or require having the file with focus (@jrial solution).
A complete configuration can be found here.

Configuration to debug any command added to option list:

{
        "name": "Django Command",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "args": [
            "${input:variableID}",
            "restaurants"
        ],
        "justMyCode": false
    }
],
"inputs": [{
    "id": "variableID",
    "description": "Select client or server",
    "type": "pickString",
    "options": ["createsuperuser", "startapp", "A CUSTOM COMMAND"],
    "default": "createsuperuser"
}]

Configuration to debug the open command file: (@jrial solution modified)

{
        "name": "Django Current Custom Command",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "args": [
            "${fileBasenameNoExtension}"
        ],
        "justMyCode": false
    }

if you don’t want to go through third party libraries code, omit the variable.

Configuration to debug the open specific custom_command.py file:(@Rik solution modified)

{
        "name": "Django Current Custom Command",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "args": [
            "custom_command",
            "THIS CAN BE A REQUIRED ARGUMENT",
        ],
        "justMyCode": false
    }

Leave a comment