Developing Django with Visual Studio - Python Django Tutorials

Developing Django with Visual Studio

Regardless of what you might hear trolling around the Internet, Microsoft Visual Studio (VS) has always been an extremely capable and powerful Integrated Development Environment (IDE). As a developer for multiple platforms, I have dabbled in just about everything else out there and have always ended up back with VS.

The biggest barriers to wider uptake of VS in the past have been (in my opinion):

  1. Lack of good support for languages outside of Microsoft’s ecosystem (C++, C# and VB).
  2. Cost of the fully featured IDE. Previous incarnations of Microsoft ‘free’ IDE’s have fallen a bit short of being useful for professional development.

With the release of Visual Studio Community Editions a few years ago and the more recent release of Python Tools for Visual Studio (PTVS), this situation has changed dramatically for the better. So much so that I now do all my development in VS – both Microsoft technologies and Python and Django.

I am not going to go on with the virtues of VS, lest I begin to sound like a commercial for Microsoft, so let’s assume that you have at least decided to give VS and PTVS a go.

Firstly, I will explain how to install VS and PTVS on your Windows box and then I will give you a quick overview of all the cool Django and Python tools that you have at your disposal.

Installing Visual Studio

Once you have taken careful note of the warning above, jump on to the Visual Studio website and download the free Visual Studio Community Edition 2015 (Figure G-1).

Developing Django with Visual Studio - Visual Studio Downloads
Figure G-1: Visual Studio Downloads

Launch the downloaded installer file, make sure the default install option is selected (Figure G-2) and click install.

Developing Django with Visual Studio - Visual Studio's default install
Figure G-2: Visual Studio’s default install

Now’s the time to go make yourself a coffee. Or seven. Microsoft, remember – it’s going to take a while. Depending on your Internet connection this can take anywhere from 15 minutes to more than an hour.

In a few rare cases it will fail. This is always (in my experience) either forgetting to turn antivirus off or a momentary dropout in your Internet connection. Luckily VS’s recovery process is pretty robust and I have found rebooting and restarting the install after a failure works every time. VS will even remember where it’s up to, so you don’t have to start all over again.

Install PTVS and Web Essentials

Once you have installed VS, it’s time to add Python Tools for Visual Studio (PTVS) and Visual Studio Web Essentials.
From the top menu, select Tools > Extensions and Updates (Figure G-3).

Developing Django with Visual Studio - Install Visual Studio's extension
Figure G-3: Install Visual Studio’s extension

Once the Extensions and Updates window opens, select “online” from the dropdown on the left to go to the VS online application gallery. Type “Python” in the search box on the top right and the PTVS extension should appear on the top of the list (Figure G-4).

Developing Django with Visual Studio - Install PTVS extension
Figure G-4: Install PTVS extension

Repeat the same process for VS Web Essentials (Figure G-5). Note that, depending on the VS build and what extensions have been installed previously, Web Essentials may already be installed. If this is the case, the “download” button will be replaced with a green tick icon.

Developing Django with Visual Studio - Install Web Essentials extension
Figure G-5: Install Web Essentials extension

Creating A Django Project

One of the great things about using VS for Django development is that the only thing you need to install other than VS is Python. So if you followed the instructions in Chapter 1 and have installed Python, there is nothing else to do – VS takes care of the virtual environment, installing any Python modules you need and even has all of Django’s management commands built in to the IDE.

To demonstrate these capabilities, lets create our mysite project from Chapter 1, but this time we will do it all from inside VS.

Start A Django Project

Select File > New > Project from the top menu and then select a Python web project from the dropdown on the left. You should see something like Figure G-6. Select a Blank Django Web Project, give your project a name and then click OK.

Developing Django with Visual Studio - Create a blank Django project
Figure G-6: Create a blank Django project

Visual Studio will then display a popup window saying that this project requires external packages (Figure G-7). The simplest option here is to install directly into a virtual environment (option 1), but this will install the latest version of Django, which at the time of writing is 1.9.7. As this book is for the 1.8 LTS version we want to select option 3 “I will install them myself” so we can make the necessary changes to the requirements.txt file.

Developing Django with Visual Studio - Install external packages
Figure G-7: Install external packages

Once the project has installed, you will notice in Solution Explorer on the right of the VS screen the complete Django project structure has been created for you. Next step is to add a virtual environment running Django 1.8. At the time of writing the latest version is 1.8.13, so we have to edit our requirements.txt file so the first line reads:


Save the file and then right click “Python Environments” in your Solution Explorer and select “Add Virtual Environment” (Figure G-8).

Developing Django with Visual Studio - Add virtual environment
Figure G-8: Add virtual environment

In the popup window, change the default environment name from “env” to something more meaningful (if you are following on from the example in Chapter 1, use “env_mysite”). Click “Create” and VS will create a virtual environment for you (Figure G-9).

Developing Django with Visual Studio - Create the virtual environment
Figure G-9: Create the virtual environment

Django Development in Visual Studio

Microsoft have put a lot of effort into ensuring developing Python applications in VS is as simple and headache free as possible. The killer feature for beginning programmers is full IntelliSense for all Python and Django modules. This will accelerate your learning more than any other feature as you don’t have to go through documentation looking for module implementations.

The other major aspects of Python/Django programming that VS makes really simple are:

  1. Integration of Django management commands.
  2. Easy installation of Python packages.
  3. Easy installation of new Django apps.

Integration of Django Management Commands

All of Django’s common management commands are available from the Project menu (Figure G-10).

Developing Django with Visual Studio - Common Django commands on Project menu
Figure G-10: Common Django commands on Project menu

From this menu you can run migrations, create superusers, open the Django shell and run the development server.

Easy Installation of Python Packages

Python packages can be installed directly into any virtual environment from Solution Explorer, just right click on the environment and select “Install Python Package…” (Figure G-11).

Developing Django with Visual Studio - Install Python package
Figure G-11: Install Python package

Packages can be installed with either pip or easy_install.

Easy Installation of New Django Apps

And finally, adding a new Django app to your project is as simple as right clicking on your project and selecting Add > Django app... (Figure G-12). Give your app a name and click “OK” and VS will add a new app to your project.

Developing Django with Visual Studio - Add a Django app
Figure G-12: Add a Django app

This is only a quick overview of the things you can do with Visual Studio; just to get you started. Other things worth exploring are:

  • VS’s repository management including full integration with local Git repos and GitHub.
  • Deployment to Azure with a free MSDN developer account (only supports MySQL and SQLite and the time of writing).
  • Inbuilt mixed-mode debugger. For example, debug Django and JavaScript in the same debugger.
  • Inbuilt support for testing.
  • Did I mention full IntelliSense support?