'How to debug Django custom management command using VS Code
I am trying to debug a custom management command using Visual Studio Code.
For this I have gone through the official VS Code tutorials for working with Python and Django, and I have managed to get debugging to work while following these tutorials.
VS Code Python tutorial / VS Code Django tutorial
The problem is that for a Python script (no Django), the debugger works because I run a specific file (by pressing f5) while the file's tab is open. Django debugging works because VS Code knows when a browser request causes my Django app to hit a break-point I entered in VS Code.
But a custom management command is run differently. For this I type the following in the console:
python manage.py name_of_management_command
How do I debug this in VS Code?
Solution 1:[1]
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
Solution 2:[2]
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.
Solution 3:[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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rik Schoonbeek |
Solution 2 | |
Solution 3 | titusfx |