How To Use Visual Studio Code With Django

How To Use Visual Studio Code With Django

Visual Studio Code is the best *free* text editor for writing code available today. It has the benefit of being lightweight, and therefore very performant, while still offering many of the features that you would normally expect in a more robust Integrated Development Environment. Visual Studio Code is also great to use when working with Python and Django since it has extensions that make it easy to code in Python and Django. Let’s get Visual Studio Code set up for working with our new Django Project now.


Install Visual Studio Code

Visual Studio Code is available for macOS, Windows x64, and Linux x64 machines as a simple installable file. Simply visit https://code.visualstudio.com/ and select the installation option that is right for you. Visual Studio Code can be used with Python, HTML, CSS, JavaScript, C++, Java, and much more. This robust ability to edit any type of programming language is possible because of the Extensions ecosystem built into Visual Studio Code. It is easy to find an extension to support almost any type of project you like.


Configuring Extensions

There are a number of extensions for Visual Studio Code that will help with Python and Django development. We can add those to our VS Code installation now.

Python Extension By Microsoft

The first extension to install to make working with Python and Django easier is the Python extension for Visual Studio Code extension. In Visual Studio Code, first, click on the Extensions icon.

vs code extensions icon

The Extensions Marketplace will expand open, and there you can type in “Python” into the search box. A large list of extensions will be displayed for you. The one you want to install has the text, “IntelliSense (Pylance), Linting, Debugging (multi-threaded, remote), Jupyter Notebooks, code formatting, refactoring, unit tests, and more.” with almost 40 million downloads. This is the official Python extension provided by Microsoft. In our case, it has already been installed, but if you need to, do click “Install” to get it installed on your system.

Python Extension for VS Code by Microsoft

Python-autopep8

The Python autopep8 extension is also a nice one to have to ensure that the current best practices for Python code formatting are applied to any Python file you work on.

python autopep8

Django Extensions

As long as you have the main Python extension installed, you will be able to work in Django Projects just fine. If you are curious, however, there are many additional Extensions that focus directly on the Django framework and some of them may be of interest to you. Installing any of these is optional.

visual studio code django extensions

Some of the more popular Django Extensions for Visual Studio Code are listed here.


Open A Django Project In VS Code

Now that the needed extensions are installed, we can open the new Django project that we recently created. To do this select File, then Open Folder, and select the outermost directory of the Django Project. When a Django project is created, there will be two folders with the same name, open the outer folder in the IDE.

vscode open django project

The resulting file structure in Visual Studio Code should be the familiar files we are used to with any Django project. These are asgi.py, settings.py, urls.py, wsgi.py, and manage.py.

django project structure


Python VS Code Integration

To ensure that Visual Studio Code is synced up with the Python installation on our machine, we’ll want to enable the Status Bar and confirm that it shows the Python installation listed as active and working.

vs code status bar python

You should then see something like the following at the bottom of the IDE, which indicates everything is ready to go.

Python Is Working

How To Use Visual Studio Code With Django Summary

Great work! Your Visual Studio Code installation is ready for working with Python and Django. Learn More About How To Use Visual Studio Code With Django: