Tuesday, April 07, 2020

Django in Visual Studio Code

Virtual environment:  
    Set up python virtual environment.

VSCode : 

1. In the view > Command Pallete, enter > Python: Select interpreter.

2. Select the one in the venv.
    Or enter python path from virtual environment.
    For example: I have env folder as virtual environment. 
    So, I select python in env\Scripts\python.exe
    

1. In the debug view (forth icon in left sidebar):
2. Create a launch json file.
3. Configure(Or click "Add Configuration..." on the bottom right)
    Select Python
    Then django

    In launch.json,
    Remove "--noreload"

    Both  settings.json and launch.json are in the project .vscode folder.

To start django:
1. Click the top left green triangle to start django.


Errors: 

1. If you have this error :

    Exception has occurred: ImportErr
    Couldn't import Django. 
    Are you sure it's installed and available on your PYTHONPATH environment variable?     ....

    then do

    pip install django.


2. How to set the port number?


The correct way to change port number is
  "configurations": [
    {
      "name""Python: Django",
      "type""python",
      "request""launch",
      "program""${workspaceFolder}\\manage.py",
      "args": [
        "runserver",
        "127.0.0.1:7000"
      ],
      "django"true,
    }
  ]


3. You may still trouble to start up the django, try to use the following  launch.json file:

{
    "version""0.2.0",
    "configurations": [
        {
            "name""Python: Django",
            "type""python",
            "request""launch",
            "program""${workspaceFolder}\\manage.py",
            "args": [
                "runserver",
                "0.0.0.0:8001"
            ],
            "django"true
        }
    ]
}

No comments: