My Visual Studio Code setup for Django
TL;DR
pip install django black isort pylint django-pylint mypy django-stubs
settings.json
{
"editor.formatOnSave": true,
"python.linting.lintOnSave": true,
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_django",
"--django-settings-module=core.settings",
"--max-line-length=120"
],
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length=120"
],
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"python.linting.mypyEnabled": true
}
mypy.ini
[mypy]
plugins =
mypy_django_plugin.main
[mypy.plugins.django-stubs]
django_settings_module = "myproject.settings"
Setting up Visual Studio Code to work on Django projects is a little different from the typical setup for pure Python projects. For example, there is little use for mypy in Django, as it doesn’t support Django types. The same is true with linters, which, without prior configuration, do not work correctly with Django code.
These problems can be solved by the configuration files of the relevant tools, but I use ready-made plugins for mypy and pylint.
What will we connect
black – package for automatic code formatting
pylint – static code analyzer
isort – package for sorting ads
import
mypy – package for checking static types
Plugins
django-pylint is a pylint plugin that improves Django code analysis.
django-stubs is a mypy plugin that provides more precise static types for Django.
New Django project
I’ll start from scratch and move on to the Visual Studio Code settings.
Installing Django and dependencies
I prefer developing new projects in containers Docker using the Visual Studio Code extension Remote Development. You can also use virtual environmentactivate it and run the command:
pip install django black isort pylint django-pylint mypy django-stubs
New Django project
For the setup, a Django starter project is enough, which can be obtained with the following command:
django-admin startproject core .
We get the directory structure:
.
├── core
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
I won’t be creating a new Django app. Instead, I’ll continue integrating the tools into this project, and you’ll later add your application to a ready-made, easy-to-use development environment.
Configuring Visual Studio Code
The settings we want to apply to the Django project are not workable without extensions Python for Visual Studio Code.
From project to project, the configuration of the tools and the tools themselves can change. Therefore, Visual Studio Code separates project settings into user (global) settings and workspace settings. Workspace settings are project-specific. They can be changed by first creating a directory in the root of the project .vscode
with file settings.json
inside. This file defines the settings for the workspace:
{
"editor.formatOnSave": true,
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length=120"
],
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_django",
"--django-settings-module=core.settings",
"--max-line-length=120"
],
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"python.linting.mypyEnabled": true
}
Turn on black
Format code while saving file (Ctrl+S):
"editor.formatOnSave": true,
Install the black format provider:
"python.formatting.provider": "black",
Black uses the default string length of 88 characters. You need to overwrite this value according to Django convention:
"python.formatting.blackArgs": [
"--line-length=120"
],
Turn on pylint
Enable linting options:
"python.linting.enabled": true,
Enable linting while saving the file (Ctrl+S):
"python.linting.lintOnSave": true,
Turn on pylint:
"python.linting.pylintEnabled": true,
Integrate the django-pylint plugin and change the line length:
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_django",
"--django-settings-module=core.settings",
"--max-line-length=120"
],
Turn on isort
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
Turn on mypy
"python.linting.mypyEnabled": true
With mypy, things are not so simple. Unfortunately, I didn’t find how to pass the plugin via command line options. This makes it inconvenient to store the configuration, since without mypy all settings are placed in one settings.json
, which allows you to simply install dependencies and import a ready-made settings file into the project. Perhaps this is a reason to fork the mypy repository and add the appropriate command line option. While writing to a file mypy.ini
at the root of the project:
[mypy]
plugins =
mypy_django_plugin.main
[mypy.plugins.django-stubs]
django_settings_module = "myproject.settings"
Ready!
Try to start writing a new application with my setup, I hope you like it! If you liked the article, subscribe to my channel in telegram, where I write about Python and not only.