从VSCODE运行Django项目

3

有没有办法从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
4个回答

5
我是这样做的: 从终端 -> 配置任务...
{
"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"
           }
       }
   ]
}

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

那么,你选择了Terminal -> Configure Tasks -> "create tasks.json file from template",然后选择了其中之一:MSBuild/maven/.NET core吗?你选择了哪一个? - jeppoo1
是的,您需要选择“从模板创建task.json文件”,然后选择“其他创建任意文件”。一个名为task.json的新文件将被创建在.vscode中,这就是您将上述字典/JSON数据粘贴到其中的地方。 - inyang kpongette

3

如果您的环境不同,请提供相应的命令。以下是关于it技术的翻译内容:

Ctrl + Shift + p,然后输入 >python: Select Interpreter,请选择安装Django的虚拟环境。

接着按下调试按钮(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终端上得到的内容。 - Filipe
1
它应该可以工作,因为我在我的电脑上使用了这个代码片段。 - Raja Simon
1
但问题是如何运行,而不是调试。 - Şah-ı Merdan

0

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

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

0

我使用VS Code,并输入python manage.py runserver运行项目,一切都很顺利。我不知道自己做了什么特别的才能让它这样工作。

我在pip虚拟环境中工作,通过输入pipenv shell启动。

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

我认为这是Django内置的功能。也许它与您安装Python的方式有关。我记得以前遇到过一些问题。那是关于在Windows环境中设置PATH变量的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接