Django Hello World Web Application
This tutorial describes how to host a Django-based web site on Windows Azure using a Windows Server virtual machine. This tutorial assumes you have no prior experience using Windows Azure. Upon completing this guide, you will have a Django-based application up and running in the cloud.
You will learn how to:
- Setup a Windows Azure virtual machine to host Django. While this tutorial explains how to accomplish this under Windows Server, the same could also be done with a Linux VM hosted in Windows Azure.
- Create a new Django application from Windows.
By following this tutorial, you will build a simple Hello World web application. The application will be hosted in a Windows Azure virtual machine.
A screenshot of the completed application is below:

Note To complete this tutorial, you need a Windows Azure account. You can create a free trial account in just a couple of minutes. For details, see Windows Azure Free Trial.
Creating and configuring a Windows Azure virtual machine to host Django
-
Follow the instructions given here to create a Windows Azure virtual machine of the Windows Server 2012 Datacenter distribution.
-
Instruct Windows Azure to direct port 80 traffic from the web to port 80 on the virtual machine:
- Use Windows Remote Desktop to remotely log into the newly created Windows Azure virtual machine.
Important Note: all instructions below assume you logged into the virtual machine correctly and are issuing commands there rather than your local machine!
Setting up the development environment
To set up your Python and Django environments, please see the Installation Guide for more information.
Note 1: you only need to install the Django product from the Windows WebPI installer on the Windows Azure virtual machine to get this particular tutorial operational.
Note 2: In order to download the WebPI installer you may have to configure IE ESC settings (Start/Adminstrative Tools/Server Manager, then click Configure IE ESC, set to Off).
Setting up IIS with FastCGI
-
Intall IIS with FastCGI support
start /wait %windir%\System32\\PkgMgr.exe /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-Security;IIS-RequestFiltering;IIS-HttpCompressionStatic;IIS-WebServerManagementTools;IIS-ManagementConsole;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI;IIS-CGI
-
Setup the Python Fast CGI Handler
%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI "/+[fullPath='c:\Python27\python.exe', arguments='C:\inetpub\wwwroot\wfastcgi.py']"
-
Register the handler for this site
%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers "/+[name='Python_via_FastCGI',path='*',verb='*',modules='FastCgiModule',scriptProcessor='c:\Python27\python.exe|C:\inetpub\wwwroot\wfastcgi.py',resourceType='Unspecified']"
-
Configure the handler to run your Django application
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Python27\python.exe', arguments='C:\inetpub\wwwroot\wfastcgi.py'].environmentVariables.[name='DJANGO_SETTINGS_MODULE',value='DjangoApplication.settings']" /commit:apphost
-
Configure PYTHONPATH so your Django app can be found by the Python interpreter
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Python27\python.exe', arguments='C:\inetpub\wwwroot\wfastcgi.py'].environmentVariables.[name='PYTHONPATH',value='C:\inetpub\wwwroot\DjangoApplication']" /commit:apphost
You should see the following:

-
Tell the FastCGI to WSGI gateway which WSGI handler to use:
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Python27\python.exe', arguments='C:\inetpub\wwwroot\wfastcgi.py'].environmentVariables.[name='WSGI_HANDLER',value='django.core.handlers.wsgi.WSGIHandler()']" /commit:apphost
-
Download wfastcgi.py from http://pytools.codeplex.com/releases/view/82132 and save it to C:\inetpub\wwwroot. This is the location the previous commands used for registering the FastCGI handler.
Creating a new Django application
-
Start cmd.exe
-
cd to C:\inetpub\wwwroot
-
Enter the following command to create a new Django project:
C:\Python27\python.exe -m django.bin.django-admin startproject DjangoApplication

The django-admin.py script generates a basic structure for Django-based web sites:
- manage.py helps you to start hosting and stop hosting your Django-based web site
- DjangoApplication\settings.py contains Django settings for your application.
- DjangoApplication\urls.py contains the mapping code between each url and its view.
-
Create a new file named views.py in the DjangoApplication subdirectory of C:\inetpub\wwwroot\DjangoApplication, as a sibling of urls.py. This will contain the view that renders the "hello world" page. Start your editor and enter the following:
from django.http import HttpResponse
def hello(request):
html = "<html><body>Hello World!</body></html>"
return HttpResponse(html) -
Now replace the contents of the urls.py file with the following:
from django.conf.urls.defaults import patterns, include, url
from DjangoApplication.views import hello
urlpatterns = patterns('',
(r'^$',hello),
) -
Finally, load the web page in your browser.

Shutting down your Windows Azure virtual machine
When you're done with this tutorial, shutdown and/or remove your newly created Windows Azure virtual machine to free up resources for other tutorials and avoid incurring Windows Azure usage charges.