Django Tutorial in Visual Studio Code
Django is a high-level Python framework designed for rapid, secure, and scalable web development. Django includes rich support for URL routing, page templates, and working with data.
In this Django tutorial, you create a simple Django app with three pages that use a common base template. You create this app in the context of Visual Studio Code in order to understand how to work with Django in the VS Code terminal, editor, and debugger. This tutorial does not explore various details about Django itself, such as working with data models and creating an administrative interface. For guidance on those aspects, refer to the Django documentation links at the end of this tutorial.
The completed code project from this Django tutorial can be found on GitHub: python-sample-vscode-django-tutorial.
If you have any problems, you can search for answers or ask a question on the Python extension Discussions Q&A.
Prerequisites
To successfully complete this Django tutorial, you must do the following (which are the same steps as in the general Python tutorial):
-
Install the Python extension.
-
Install a version of Python 3 (for which this tutorial is written). Options include:
- (All operating systems) A download from python.org; typically use the Download Python 3.9.1 button that appears first on the page (or whatever is the latest version).
- (Linux) The built-in Python 3 installation works well, but to install other Python packages you must run
sudo apt install python3-pipin the terminal. - (macOS) An installation through Homebrew on macOS using
brew install python3(the system install of Python on macOS is not supported). - (All operating systems) A download from Anaconda (for data science purposes).
-
On Windows, make sure the location of your Python interpreter is included in your PATH environment variable. You can check the location by running
pathat the command prompt. If the Python interpreter's folder isn't included, open Windows Settings, search for "environment", select Edit environment variables for your account, then edit the Path variable to include that folder.
Create a project environment for the Django tutorial
In this section, you create a virtual environment in which Django is installed. Using a virtual environment avoids installing Django into a global Python environment and gives you exact control over the libraries used in an application. A virtual environment also makes it easy to Create a requirements.txt file for the environment.
The Python Environments extension supports multiple environment types including venv, conda, poetry, and others. This tutorial uses venv because it's built into Python and requires no additional tools. The steps for other environment types are similar — see Create environments for details.
-
On your file system, create a project folder for this tutorial, such as
hello_django. -
Open the project folder in VS Code by running
code ., or by running VS Code and using the File > Open Folder command. -
Create a virtual environment using the Python: Create Environment command:
- Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P))
- Search for and select Python: Create Environment
- Select Venv to create a venv environment
- Select a Python interpreter to use for the environment
VS Code creates a
.venvfolder in your workspace and automatically selects the new environment.TipYou can also create environments by using the Python sidebar. Expand Environment Managers and select the + button for Quick Create, which uses sensible defaults.

-
The selected environment appears on the right side of the VS Code status bar, with the ('.venv': venv) indicator showing that you're using a virtual environment:

-
Install Django in the virtual environment using one of these methods:
Using the Package Management UI:
- In the Python sidebar, expand Environment Managers
- Right-click on your
.venvenvironment and select Manage Packages - Search for
djangoand select Install
Using the terminal:
Run Terminal: Create New Terminal (⌃⇧` (Windows, Linux Ctrl+Shift+`)) from the Command Palette, which creates a terminal and automatically activates the virtual environment. Then run:
python -m pip install django
You now have a self-contained environment ready for writing Django code. VS Code activates the environment automatically when you open a new terminal. If you open a separate command prompt or terminal outside VS Code, activate the environment by running source .venv/bin/activate (Linux/macOS) or .venv\Scripts\Activate.ps1 (Windows). You know the environment is activated when the command prompt shows (.venv) at the beginning.
Create and run a minimal Django app
In Django terminology, a "Django project" is composed of several site-level configuration files, along with one or more "apps" that you deploy to a web host to create a full web application. A Django project can contain multiple apps, each of which typically has an independent function in the project, and the same app can be in multiple Django projects. An app, for its part, is just a Python package that follows certain conventions that Django expects.
To create a minimal Django app, then, it's necessary to first create the Django project to serve as the container for the app, then create the app itself. For both purposes, you use the Django administrative utility, django-admin, which is installed when you install the Django package.
Create the Django project
-
In the VS Code Terminal where your virtual environment is activated, run the following command:
django-admin startproject web_project .This
startprojectcommand assumes (by use of.at the end) that the current folder is your project folder, and creates the following within it: