[PYTHON] I tried starting Django's server with VScode instead of Pycharm

introduction Recently, I'm Marun (provisional), an engineer who started Django for self-development for 3 days. We are looking for a handle name ('ω') [Get started with Python Django development](https://www.amazon.co.jp/%E5%8B%95%E3%81%8B%E3%81%97%E3%81%A6%E5% AD% A6% E3% 81% B6-Python-Django% E9% 96% 8B% E7% 99% BA% E5% 85% A5% E9% 96% 80-NEXT-ONE / dp / 4798162507 / ref = tmm_pap_swatch_0? I bought _encoding = UTF8 & qid = & sr =) and started. In the book, I was developing with Pycharm and starting the server, and as a person who is developing mainly with VScode recently, I felt like hmm. I think it's good to fix the code, but I wonder if VScode can somehow start the server. So I implemented it using the debugging features I learned at VScode Meetup on December 18th! Since the introduction has become long, I will write it immediately.

environment

1. Interpreter settings

Since I was developing using venv, I created a new settings.json and reconfigured venv's Python.exe as follows.

/Users/marun/test/.vscode/settings.json


{
    "files.eol": "\n",
    "terminal.integrated.env.windows": {
        "PATH": "${workspaceFolder}/venv/Scripts;${env:PATH}"
    },
    // ${workspaceFolder}:C:Users/marun/It is a test.
    "python.pythonPath": "${workspaceFolder}/venv/Scripts/python.exe"
}

2. Setting environment variables

I needed an environment variable to start Django's server on Windows, but it seems that launch.json can read the .env file, so I'll create it.

/Users/marun/test/.vscode/.env


PYTHONUNBUFFERED=1
DB_USER="[User name]"
DB_PASSWORD="[password]"

This is your own setting, so rewrite the contents as necessary. Doesn't it need environment variables for mac?

3. Create launch.json

Finally, we will create launch.json, which is the basis for configuring the debug function.

/Users/marun/test/.vscode/launch.json


{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload"
            ],
            "envFile": "${workspaceFolder}/.vscode/.env",
            "django": true
        }
    ]
}

By defining envFile, I was able to read the environment variables safely. If you create this, you'll see a button like the one below in debug mode.

VScode1.png

4. Run

Press the button and the terminal will start moving. If there is no error and the output is as follows, it is successful! Let's actually hit the URL surrounded by red to check it! VScode2.png

5. Finally

It's the second time I've written an article on Qiita, and I'm still lacking in study, so I can't say that I'm an engineer, but I'll continue to implement and output what I wish I had. I think. May my fellow countrymen with similar worries be saved in this article. Also, I am waiting for comments such as that it is better to do this or that this is wrong! Give me your knowledge! !!

Digression

[Get started with Python Django development](https://www.amazon.co.jp/%E5%8B%95%E3%81%8B%E3%81%97%E3%81%A6%E5% AD% A6% E3% 81% B6-Python-Django% E9% 96% 8B% E7% 99% BA% E5% 85% A5% E9% 96% 80-NEXT-ONE / dp / 4798162507 / ref = tmm_pap_swatch_0? _encoding = UTF8 & qid = & sr =), but I think it's a very good book because it's full color and you can learn various techniques other than Django (arguments are welcome).

Digression Digression

How do people look for a handle name or a nice icon? .. ..

Recommended Posts

I tried starting Django's server with VScode instead of Pycharm
I tried the asynchronous server of Django 3.0
I tried handwriting recognition of runes with scikit-learn
I tried hundreds of millions of SQLite with python
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried Flask with Remote-Containers of VS Code
I tried using firebase for Django's cache server
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried using Python (3) instead of a scientific calculator
I tried "morphology conversion" of images with Python + OpenCV
I tried fp-growth with python
I tried scraping with Python
I tried "gamma correction" of the image with Python + OpenCV
I tried to find the average of the sequence with TensorFlow
I tried to rewrite the WEB server of the normal Linux programming 1st edition with C ++ 14
I tried Learning-to-Rank with Elasticsearch!
I tried running Movidius NCS with python of Raspberry Pi3
I tried clustering with PyCaret
I tried to implement ListNet of rank learning with Chainer
I tried gRPC with Python
I tried handwriting recognition of runes with CNN using Keras
I tried scraping with python
I tried a stochastic simulation of a bingo game with Python
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried standalone deployment of play with fabric [AWS operation with boto] [Play deployment]
I tried to automate the watering of the planter with Raspberry Pi
I tried to create a list of prime numbers with python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried sending an email from the Sakura server with flask-mail
I tried to expand the size of the logical volume with LVM
I tried running the DNN part of OpenPose with Chainer CPU
I tried to improve the efficiency of daily work with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to make a mechanism of exclusive control with Go
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
I tried machine learning with liblinear
I tried web scraping with python.
I tried moving food with SinGAN
I tried using GrabCut of OpenCV
I tried implementing DeepPose with PyTorch
I tried face detection with MTCNN
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
I tried face recognition with OpenCV
I tried to notify Zabbix Server of execution error of AWS Lambda function
I tried to communicate with a remote server by Socket communication with Python.
I tried a formation flight of a small drone Tello with ESP32: DJI Tello drone formation flight
I tried to get the authentication code of Qiita API with Python.
I tried Linux (CentOS 7) life and death monitoring (Ping) with monitoring server Zabbix
I tried to automatically extract the movements of PES players with software
I tried to analyze the negativeness of Nono Morikubo. [Compare with Posipa]
A reminder of what I got stuck when starting Atcoder with python
I tried to streamline the standard role of new employees with Python
I tried to visualize the text of the novel "Weathering with You" with WordCloud
I tried to get the movie information of TMDb API with Python
I tried to visualize all decision trees of random forest with SVG
I tried to predict the behavior of the new coronavirus with the SEIR model.