使用 vs code 創建 Django 項目


操作流程:


1、前期准備工作

  • 安裝Python,使用pip安裝pylintyapfpip install pylint yapf
  • 下載安裝vscode:https://code.visualstudio.com/
  • 打開vscode,點擊左側 擴展 圖標按鈕,點擊 更多 選擇 顯示常用的擴展 ,選擇並安裝插件 Python (Microsoft官方發布),安裝完成后點擊 重新加載 即可重啟vscode並激活 Python 插件
  • 安裝flake8之后寫代碼的時候編輯器就會提示哪里出錯,代碼格式不規范也會提示
  1. 打開命令行
  2. 輸入 pip install flake8
  3. 安裝flake8成功后,打開VScode,文件->首選項->用戶設置,在settings.json文件中輸入"python.linting.flake8Enabled": true

2、vs code配置Python環境]

  • settings.json設置如下:
{
    "python.pythonPath": "E:/python/python.exe",
    "python.autoComplete.extraPaths": [
        ".../python",
        ".../python/Lib/site-packages",
        ".../python/Scripts"
    ],
    "python.linting.pylintEnabled": true,
    "python.formatting.provider": "yapf",
    "python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 4}"],
    "python.linting.pylintArgs": [
        "--load-plugins", "pylint_django"
    ]
}

3、新建 Django 項目

  • 使用 vs code打開新建的文件,如下:

  • 打開 vs code 的 Terminal 窗口,切換路徑到項目路徑下, 執行以下命令:

django-admin startproject project_name

  • 繼續執行以下命令:
python manage.py startapp app_name

  • 備注:

(1)HelloWorld:項目的容器

(2)manage.py:一個實用的命令行工具,可讓你以各種方式與該Django項目進行交互。

(3)HelloWorld/init.py:一個空文件,告訴Python該目錄是一個Python包

(4)HelloWorld/settings.py:該Django項目的設置/配置。

(5)HelloWorld/urls.py:該Django項目的URL聲明,一份由Django驅動的網站“目錄”

(6)HelloWorld/wsgi.py:一個WSGI兼容的Web服務器的入口,以便運行你的項目


4、vs code 配置 Debug Django 環境

  • launch.json設置如下:
{
    // 使用 IntelliSense 了解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [{
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost"
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name-here",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/HelloWorld/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "8080", //配置Django端口
                "--noreload",
                "--nothreading"
            ],
            "django": true
        },
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}

5、瀏覽器查看效果




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM