【问题标题】:Run a Django project from VSCODE从 VSCODE 运行 Django 项目
【发布时间】:2018-05-24 13:09:46
【问题描述】:

有没有办法从 VSCODE 编辑器运行 Django 项目?

目前我使用 windows CMD 来运行我的项目。每次我点击 VSCODE 上的“运行”按钮时,它都不会运行项目。

在 pyCharm 上,我们可以将“runserver”添加到其脚本参数配置中。 VSCODE 上有类似的东西吗?

更新

我在 VSCODE 终端上得到这个:

Type 'manage.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[contenttypes]
    remove_stale_contenttypes

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver

[Done] exited with code=0 in 9.449 seconds

【问题讨论】:

    标签: django visual-studio-code vscode-settings


    【解决方案1】:

    我在Linux,如果你是不同的环境请提供适当的命令

    Ctrl + Shift + p 并输入 >python: Select Interpreter 请选择 Django 安装的 virtualenv。

    然后转到调试 (Ctrl + Shift + d) 和这些...

    {
        // 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": "Django",
                "type": "python",
                "request": "launch",
                "stopOnEntry": true,
                "pythonPath": "${config:python.pythonPath}",
                "program": "${workspaceRoot}/manage.py",
                "cwd": "${workspaceRoot}",
                "args": [
                    "runserver",
                    "--noreload",
                    "--nothreading"
                ],
                "env": {},
                "envFile": "${workspaceRoot}/.env",
                "debugOptions": [
                    "WaitOnAbnormalExit",
                    "WaitOnNormalExit",
                    "RedirectOutput",
                    "DjangoDebugging"
                ]
            }
        ]
    }
    

    【讨论】:

    • 感谢您的回答。虽然我仍然不能直接从 VSCODE 运行项目。我更新了问题以显示我在 VSCODE 终端上得到了什么。
    • 它应该可以工作,因为我在我的电脑中使用这个 sn-p
    • 但问题是如何运行,而不是调试。
    【解决方案2】:

    这是我的做法: 从终端 -> 配置任务...

    {
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Django: Run Server",
            "type": "shell",
            "command": "${config:python.pythonPath}",
            "args":[ 
                   "manage.py",
                   "runserver"
             ],
            "group": "none",
            "presentation": {
                "reveal": "always",
                "panel": "new"
               }
           }
       ]
    }
    

    之后,它应该会出现,当您选择终端 -> 运行任务...

    【讨论】:

    • 那么,您是否选择了终端 -> 配置任务 -> “从模板创建 tasks.json 文件”,然后选择了以下三个之一:MSBuild/maven/.NET 核心?你选了哪个?
    • 是的,您要选择“从模板创建 task.json 文件”,然后选择“其他人创建任意文件”。将在 .vscode 中创建一个名为 task.json 的新文件,您将在此处粘贴上述字典/json 数据。
    【解决方案3】:

    如果不想更新 VS Code 中的改动,可以在 Windows 终端运行命令:

    • 第 1 步:激活虚拟环境(如果您正在使用)
    • 第二步:指向manage.py所在的目录,然后运行runserver命令就可以了。

    【讨论】:

      【解决方案4】:

      我使用 VS Code 并输入 python manage.py runserver 运行项目,一切顺利。我不知道有什么特别的事情让它以这种方式工作。 我确实在通过键入pipenv shell 启动的 pip 虚拟环境中工作。

      每当我运行 python manage.py runserver 时,它都会在 127.0.0.1:8000 启动服务器。

      我以为它是 django 内置的。也许它与您如何安装python有关。我记得前段时间遇到了一些问题。这是关于在 windows 环境中设置 PATH 变量。

      【讨论】:

        猜你喜欢
        • 2022-11-03
        • 2022-01-18
        • 2020-01-21
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        相关资源
        最近更新 更多