How to debug Django custom management command using VS Code

ⅰ亾dé卋堺 提交于 2020-08-24 09:00:29

问题


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?


回答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




回答2:


Rik's answer is correct, but requires you to create a debug entry for each individual management command.

The name of the management command is the same as its filename, without the .py extension. In order 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}"
            ]
        }

For more variables you can use in your launch configuration, apart from ${fileBasenameNoExtension}, see https://code.visualstudio.com/docs/editor/variables-reference



来源:https://stackoverflow.com/questions/56008730/how-to-debug-django-custom-management-command-using-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!