简体   繁体   English

如何使用visual studio代码调试django

[英]How to use visual studio code to debug django

I'm new at django development and come from desktop/mobile app development with Xcode and related IDE.我是Django开发的新手,来自使用Xcode和相关 IDE 进行桌面/移动应用程序开发。

I have to use Django and I was wondering if there was an efficient way to debug it using Visual Studio Code (or Atom ).我必须使用 Django,我想知道是否有一种使用Visual Studio Code (或Atom )调试它的有效方法。

Any help related to Django IDE would be helpful too.任何与 Django IDE 相关的帮助也会有所帮助。

For VSCode (full disclosure, I'm one of the VSCode developers) try installing the Python extension to get started.对于 VSCode(完全公开,我是 VSCode 开发人员之一)尝试安装Python 扩展以开始使用。

This documentation covers debugging Django . 本文档涵盖调试 Django There should be a included debug configuration or you can add your own to the launch.json file :应该包含调试配置,或者您可以将自己的配置添加到launch.json文件

{
    "name": "Django",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "${config.python.pythonPath}",
    "program": "${workspaceRoot}/manage.py",
    "args": [
        "runserver",
        "--no-color",
        "--noreload"
    ],
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput",
        "DjangoDebugging"
    ]
}

The Python extension also provide many other features that you may find useful. Python 扩展还提供了许多您可能会觉得有用的其他功能。

Hope that helps you get started.希望能帮助您入门。

VSCode has an official tutorial explaining this: VSCode 有一个官方教程解释了这一点:

https://code.visualstudio.com/docs/python/tutorial-django https://code.visualstudio.com/docs/python/tutorial-django

There are several steps that need to be taken, which I don't all want to write out manually, since there are quite some steps, but I'll try to summarize what needs to be done:有几个步骤需要采取,我不想都手动写出来,因为步骤相当多,但我会尽量总结需要做的事情:

The text below is basically a partial copy of the above tutorial, I am not claiming I came up with this myself.下面的文字基本上是上述教程的部分副本,我并不是说这是我自己想出来的。

1. Make sure to check out the prerequisites (use VS Code Python extension, install Python on local machine) link to docs 1. 确保查看先决条件(使用 VS Code Python 扩展,在本地机器上安装 Python) 链接到文档

2. Use Python virtual environment link to docs 2.使用Python虚拟环境链接到文档

Besides using a Python virtual environment, you also need to select the Python executable inside this virtual environment as the interpreter in VS Code.除了使用 Python 虚拟环境之外,还需要选择这个虚拟环境中的 Python 可执行文件作为 VS Code 中的解释器。 This can be done like so:这可以像这样完成:

In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)).在 VS Code 中,打开命令面板(查看 > 命令面板或 (Ctrl+Shift+P))。 Then select the Python: Select Interpreter然后选择 Python:选择解释器

Then you select the Python executable inside your virtual environment, which you can recognize by it's path.然后你在你的虚拟环境中选择 Python 可执行文件,你可以通过它的路径来识别。

3. Create debugger lauch profile 3. 创建调试器 lauch 配置文件

as described here, in the documentation 如此处所述,在文档中

upper left of the VS Code window) VS Code 窗口的左上角)

4. Now you can start debugging 4. 现在可以开始调试了

this part of the documentation will give you an introduction on how to do that 文档的这一部分将向您介绍如何做到这一点

Only experimental configuration works for me.只有实验配置对我有用。

{
            "name": "Django",
            "type": "pythonExperimental",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
},

Standard config causes Unverified breakpoint issue.标准配置导致未Unverified breakpoint问题。

在我禁用自动重新加载之前,没有什么对我--noreload--noreload作为参数至关重要,不确定为什么会导致调试问题)

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "runserver"
            ],
            "django": true
        },
        {
            "name": "Django: makemigrations",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "makemigrations"
            ],
            "django": true
        },
        {
            "name": "Django: migrate",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "migrate"
            ],
            "django": true
        },
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM