Debug Django Management Commands In VSCode
Adding break points to a django management command in Visual Studio Code is one of those tasks that I’ve been wanting to achieve for a while but never really got around to investigating. I generally fell back on print statements which has been Good Enough.
VSCode does give you some helpful templates for debugging commands when setting them up. It already has a pretty workable Django server solution but nothing specifically in the way of management commands. However the entry for “Python File With Arguments” looked interesting and that seemed like a good stepping stone into what I wanted.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File with Arguments",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": "${command:pickArgs}"
}
]
}
This is pretty close. What we want to do is tweak the program entry so that it follows the standard ./manage.py
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Django Management Command",
"type": "debugpy",
"request": "launch",
// Make sure the path to `manage.py` is correct for your project
"program": "${workspaceFolder}/backend/manage.py ${fileBasenameNoExtension}",
"console": "integratedTerminal",
"args": "${command:pickArgs}"
}
]
}
This way we can set break points inside a management command file and run the currently open file from the debugging options. You’ll also have options to add arbitrary arguments too.