Getting Started with Django & Windows the Easy way

It used to be that developing in Python on Windows required a bunch of setup (installing Python, pip, setting up your PATH) and managing virtual environments. The days of this are over though with the latest update to Microsoft’s Visual Studio Code.

The Visual Studio Code Remote update adds the ability to develop using a virtual machine, physical remote machine, Windows Subsystem for Linux, or a Docker container. It feels like you’re developing on your local environment, but you have access to all the tools installed on your remote environment!

So what does this mean for Python and Django development? For starters, you no longer have to spend time configuring your local machine or worrying about messing up your perfectly good development environment! In this article I’ll show you how to spin up a Docker/Python development environment in a few minutes!


Requirements

You’ll need the following tools installed to follow this tutorial:

  • Visual Studio Code Insiders
  • Docker (make sure to follow the steps Docker gives you for enabling HyperV)

Setting up your development environment

Open up Visual Studio Code Insiders and install the “Remote Development” extension pack from Microsoft.

You can find the Remote Development extension pack from the extensions marketplace within VS Code

Once the extension is installed, open the command palette (CTRL + SHIFT+ P) and choose “Open Folder in Container”.

Use CTRL+SHIFT+P and choose “Open Folder in Container”

Create a new folder and then pick “Python 3” from the next drop-down that appears.

Select Python 3

VS Code will now fetch and start a Python 3 Docker container. If you check the directory structure, you’ll now see a “.devcontainer” folder.

The .devcontainer folder that Code creates

Code uses this to store the Dockerfile and a few other files that customizes VS Code for the remote environment.

“devcontainer.json” tracks the folder being used, location of the Dockerfile, and the extensions you have installed for the development environment. Code allows you to install extensions on your local environment, or on the remote environment. Extensions installed on the remote environment are able to take advantage of the tooling installed on the environment (for example, the Python extension will use the version of Python installed in our Docker container).

“settings.vscode.json” is a remote specific VS Code settings file. Use this to configure remote environment extensions.

Installing Django

Next up we’ll install Django on our Docker container. To do this, simply open the terminal (CTRL + ` ) and create a new terminal instance (+ button in the top right of the terminal).

Type pip install Django to install the Django framework.

Install Django through the VS Code terminal with “pip install django”

Since the VS Code terminal is pointing to our Docker container, pip will install Django inside of our new Python development environment.

Once it’s finished, you can type python -m django --version to ensure that Django is now available.

Scaffolding and running our project

In the terminal, type django-admin startproject codetest to scaffold a new Django project in the current directory.

Scaffold a new Django project inour development environment

Next, we’ll setup our debugger and launch our Django project. Go to the debug tab, and click the “No Configurations” list at the top. Select Django from the next list.

Add a Django debug configuration

Finally, change the path to be ${workspaceFolder}/codetest/manage.py so it reflects our directory structure.

Update the path to reflect our directory structure

Now press the run button and VS Code will start up our Django project!

Output in the terminal after VS Code starts the Django project

If you try to visit http://127.0.0.1:8000/ in a browser though you’ll get the following screen:

:-(

This is because Python is running inside our Docker container, not our local machine so we can’t access the server from our web browser. Thankfully Code makes this easy to fix!

Click the “Dev Container:Python 3” message in the bottom status bar:

And choose “Forward Port from Container…”

We want to expose a port from our Docker container to our local environment

VS Code will recognize that Python is using port 8000 and offer it as a port to forward.

VS Code will detect ports available to forward

Go ahead and select this port.

You can click “Open Browser” to quickly see your Django app running

Now, if you visit http://127.0.0.1:8000 in your browser, you’ll see the Django welcome screen 🚀

Django welcome screen!

That’s it! You now have a fully functioning Django environment, and you didn’t have to install a single Python related thing on your local environment 🎉

The next time you open this folder, Code should automatically spin up your Docker container. If not though, you can choose “Reopen this folder in container” from the command palette.

The command palette also has an option to reopen the folder locally if you are already developing remotely