python 如何使用visual studio代码调试django

inn6fuwd  于 2022-12-25  发布在  Python
关注(0)|答案(5)|浏览(215)

我是django开发的新手,来自Xcode和相关IDE的桌面/移动的应用程序开发。
我不得不使用Django,我想知道是否有一种有效的方法来使用Visual Studio代码(或Atom)调试它。
任何与Django IDE相关的帮助也会很有帮助。

smdnsysy

smdnsysy1#

对于VSCode(完全公开,我是VSCode开发人员之一),请尝试安装Python extension来开始。
This documentation covers debugging Django。应该包含调试配置,或者您可以将自己的配置添加到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"
    ]
}

Python扩展还提供了许多其他有用的特性。

r55awzrz

r55awzrz2#

VSCode有一个官方教程解释这一点:
https://code.visualstudio.com/docs/python/tutorial-django
有几个步骤需要执行,我不想全部手动写出,因为有相当多的步骤,但我将尝试总结需要执行的操作:

  • 下面的文本基本上是上述教程的部分副本,我并不声称我自己想出了这个。*
    1.确保检查先决条件(使用VS Code Python扩展,在本地计算机上安装Python)文档链接
    2.使用Python虚拟环境链接到文档

除了使用Python虚拟环境之外,你还需要选择这个虚拟环境中的Python可执行文件作为VS代码中的解释器,可以这样做:
在VS代码中,打开命令面板(视图〉命令面板或(Ctrl+Shift+P)),然后选择Python:选择口译员
然后在虚拟环境中选择Python可执行文件,您可以通过其路径识别它。

3.创建调试器启动配置文件

如本文文档中所述
VS代码窗口的左上角)

4.现在可以开始调试了

本文档的这一部分将给予您介绍如何执行此操作

3htmauhk

3htmauhk3#

只有实验性配置对我有效。

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

标准配置导致Unverified breakpoint问题。

juud5qan

juud5qan4#

{
    // 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
        },
    ]
}
kqlmhetl

kqlmhetl5#

在我禁用自动重载之前,什么都不起作用(--noreload作为参数是至关重要的,不确定为什么它会导致调试问题)

相关问题