Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

The Ultimate Visual Studio Code Setup For Django Developers

4 min read

Visual Studio the text editor is known as Visual Studio Code is Microsoft’s free text editor that runs on Windows, Linux, and macOS. It’s a recent entrant to the market; Microsoft released the product as a public preview at the end of 2015, posting the open source code to Github, before making it available as a general release in April 2016.

Despite its newbie status, Visual Studio Code has rapidly gained popularity among developers. Some may argue that it is not a real IDE, but merely an advanced text editor. But in my opinion, after installing a number of extensions, it becomes almost a full-fledged IDE with very rich functionality.

Additionally, despite being an Electron-based application, it is quite lightweight and responsive (in contrast to for example Atom, which is very slow and resource intensive).

This tutorial will go through Installation and setup of the VS code for Python and Django projects on your machine.

Installing Visual Studio Code

Visual Studio Code is a free text editor so to download it you just have to visit their official site and download the file depending on your operating system. So visit Vscode's-website and download the latest stable build for your OS and once the download is finished install the editor and launch the app.

Getting Started with Python in VS Code

The best thing about VScode is that it comes with a built-in terminal which comes handy for Django projects press Ctrl+Shift+` to invoke the terminal.

Vs code setup for python

Note that in windows powershell may seem weird for new users it is recommended to use Python debug console or CMD.

Configuring Python

To enjoy Pythonic features such as Linting, Debugging (multi-threaded, remote), Intellisense, code formatting, refactoring, unit tests, snippets, and more you need to install Python extension for Vscode.

To install an extension press Ctrl+Shift+x or click the extension icon.

Using VS Code for Python/Django Development

Now search for Python and install the one published by Microsoft.

Python for vscode

Adding Extension For Django

Search for Djaneiro this extension provides a collection of snippets for Django templates, models, views, fields & forms ported from Djaneiro for SublimeText.

Selecting Python Environment

Press CTRL+SHIFT+P(CMD+SHIFT+P for MacOS) and type Python: Select Interpreter and select the environment for your project. You can see the active environment at the bottom left of the editor.

Vs code python environment

Installing Theme

The default Vscode theme is great in itself however there are plenty for fabulous free themes available for download.

My favorite one is Night Owl+ so let's install night owl+ theme from extensions.

To explore more Vscode themes visit https://vscodethemes.com/

vscode theme

To change the theme go to, file>Preference> color theme 

vscode change theme

And select the theme that you prefer.

Vscode theme selection

Notice below the color theme there is an option for the file icon theme.

Download Material Icon Theme from extensions store and click on that and select the icon theme this will give different file icons for different files.

file icon

After that restart VS code to activate the extensions. Now open any of your Django project you should get this beautiful view.

Vscode django setup

Your font might look different I am using Fira code font here.

 

Configuring  Additional Useful Settings

Go to file> preferece> settings then open settings.json from there.

settings.json python vscode

In USER SETTINGS inside the curly braces { } add the following Settings.

// my custom settings
    "editor.formatOnSave": true,
    "editor.rulers": [        80,        120    ],
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        ".vscode": true,
        "**/*.pyc": true,
    },
    "workbench.editor.enablePreview": false,
    "editor.minimap.enabled": false,
    "python.linting.pylintArgs": 
[        "--load-plugins",
        "pylint_django"
    ]

To use the above features, the editor will prompt you to install pylint and autopep8, or you can install them directly in the virtual environment.

pip install autopep8pip install pylint

Some useful VScode extensions:

  • Code Spell Checker 
  • Django
  • Git History


TOOLS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read