Sunteți pe pagina 1din 10

Python Web Sites with Django - Azure tutorial

1 of 10

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...
MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

(/en-us/)

Features

FREE TRIAL
Pricing

Documentation (/en-us/documentation/)

Gallery (/en-us/gallery/)

Community

Downloads (/en-us/downloads/)

Support

Like

22

(/en-us/pricing
/free-trial/)
Tweet 4

Discover more resources for these services: Web Sites (/en-us/documentation/services/web-sites/)


Discover more resources: Compute (/en-us/develop/python/compute/)
Data Services (/en-us/develop/python/data-services/) App Services (/en-us/develop/python/app-services/)

Creating Web Sites with Django


This tutorial describes how to get started running Python on Azure Web Sites. Azure Web Sites provide limited free hosting
and rapid deployment, and now, you can use Python! As your app grows, you can switch to paid hosting, and you can also
integrate with all of the other Azure services.
This tutorial shows you how to deploy an application built using the Django web framework. The tutorial walks through the
steps of deploying your application and any required libraries, including Django. You will put this all into a Git repository which
makes it quick and simple to push updates to your web site. And finally, you will configure the newly created site via Azure so
that it runs your Python application.

NOTE:
To complete this tutorial, you need an Azure account. You can activate your MSDN subscriber benefits (/en-us
/pricing/member-offers/msdn-benefits-details/) or sign up for a free trial (/en-us/pricing/free-trial/).

This tutorial uses Python 2.7 and Django 1.4. You can either get these on your own, or you can quickly and easily install these
by using the Windows Installer link on http://www.windowsazure.com/en-us/develop/python/ (http://www.windowsazure.com
/en-us/develop/python/).
Note: Azure web sites now come with Python (2.7.3 or 3.4.0, your choice) and wfastcgi handler pre-installed. However, web
frameworks such as Django are not included. You can still use a different Python interpreter if you prefer. You just need to
include it in the Git repository and configure the web site to use that interpreter instead of the Python 2.7 interpreter already
installed.

NOTE:
You can now select the version of Python that you want to use in the Azure Web Sites portal by opening the
Configure tab of your web site and changing the Python Version setting.

You will also need to install a deployment option for pushing the site to Azure. There are various deployment tools available,
but this tutorial uses Git. We recommend msysgit (http://code.google.com/p/msysgit/).
Note: TFS publishing is currently not supported for Python projects.
Once you have Python, Django, and Git installed, you will have everything you need to get started.

Web Site Creation on Portal


The first step in creating your app is to create the web site via the Azure Management Portal. To do this, you will need to login
to the portal and click the NEW button in the bottom left corner. A window will appear. Click Quick Create, enter a URL, and
select Create Web Site.

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

2 of 10

Community

Support

Downloads (/en-us/downloads/)

(/en-us/pricing
/free-trial/)

The site will be quickly set up. Next, you will add support for publishing via Git. This can be done by choosing Set up
deployment from source control.

From the Set up deployment dialog, scroll down and select the Local Git option. Click the right arrow to continue.

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

3 of 10

Community

Downloads (/en-us/downloads/)

Support

(/en-us/pricing
/free-trial/)

After setting up Git publishing, you will momentarily see a page informing you the repo is being created. When the repo is
ready, you will be taken to the deployments tab. The deployments tab includes instructions on how to connect.

Web Site Development


Now that you have created your Git repository in Azure, you will start filling it in with the web site from your local machine.
The first step is to clone the existing empty site using the url provided:

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

4 of 10

Community

Downloads (/en-us/downloads/)

Support

(/en-us/pricing
/free-trial/)

From here, you are ready to setup the enlistment with the web site. You will need to do a few things:
1. Include the Django library and other libraries that you will be using to run the web site.
2. Include the Django application code.
First, include the Django library. To do this, create a new directory called site-packages, and copy your installed version of
Django there by using these commands:
mkdir site-packages
cd site-packages
xcopy /s C:\Python27\lib\site-packages\* .
These commands copy all the libraries located in site-packages, including Django. If there are libraries that are not used by
your web site, feel free to remove them.

Next, create your initial Django application. You can do this just as you would create any other Django application from the
command line, or you can use Python Tools for Visual Studio (http://pytools.codeplex.com/) to create the project. Both
options are shown here.
Option 1: To create the new project from the command line, run the following command. The command creates the Django
application in the DjangoApplication folder:
C:\Python27\python.exe -m django.bin.django-admin startproject DjangoApplication

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

5 of 10

Community

Downloads (/en-us/downloads/)

Support

(/en-us/pricing
/free-trial/)

Option 2:
You can also create your new site using Python Tools for Visual Studio. Start Visual Studio with Python Tools for Visual Studio
installed and select File->New Project. Under Other Languages, drill into the Python projects and select Django Application.
Enter DjangoApplication for the name of the project, and make sure that Create directory for solution is unchecked to get
the exact same directory structure as when you create a Django application from the command line. This option will set you
up with a Visual Studio solution and project file that give you a great local development experience that includes template
debugging and intellisense.

Now, you just need to add all of the files you have just added and push the site to Git. To do this, run these commands:
git add DjangoApplication site-packages
git commit -m "Initial site"
git remote add azure https://dinov@pythonwebsite.scm.azurewebsites.net/PythonWebSite.git
git push azure master
The first command will add your untracked files to be tracked. The second command will commit the files you just added into
the repository. The third command adds a remote with the name azure for your repository. The final command takes the
changes and pushes them to the remote repository. This last command will also start the deployment. After doing this, you
should see a result similar to this:

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

6 of 10

Community

Downloads (/en-us/downloads/)

Support

(/en-us/pricing
/free-trial/)

After doing the push, you will see the Azure portal refresh and display the active deployment:

Web Site Configuration


Now you need to configure the web site so that it will know about your Django project and use the wfastcgi handler. To do
this, you can click on the Configure tab along the top of the screen, and then scroll down to the bottom half of the page
which contains the app settings and handler mappings sections.
All of the settings that are set here will turn into environment variables during the actual request. This means that you can use
this to configure the DJANGO_SETTINGS_MODULE environment variable as well as PYTHONPATH and WSGI_HANDLER. If
your application has other configuration values, you could assign these here and pick them up out of the environment.
Sometimes you will want to set something which is a path to a file in your web site. For example, you will want to do this for
PYTHONPATH. When running as an Azure web site, your web site will live in D:\home\site\wwwroot, so you can use that in
any location where you need a full path to a file on disk.
To set up a Django application, you need to create three environment variables. The first is DJANGO_SETTINGS_MODULE,
which provides the module name of the Django application which will be used for configuration. The second is the
PYTHONPATH environment variable that specifies the package that the settings module lives in. The third is WSGI_HANDLER.
This variable is a module/package name, followed by the attribute in the module to be used (for example,
mypackage.mymodule.handler). Add parentheses to indicate that the attribute should be called. So for these variables, set
them up as:

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

DJANGO_SETTINGS_MODULE
DjangoApplication.settings
MY ACCOUNT (/EN-US/ACCOUNT/)
PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)
PYTHONPATH

D:\home\site\wwwroot\DjangoApplication;D:\home\site\wwwroot

\site-packages

WSGI_HANDLER
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

7 of 10

django.core.handlers.wsgi.WSGIHandler()

Community

Downloads (/en-us/downloads/)

Support

FREE TRIAL

(/en-us/pricing
/free-trial/)

Now you need to configure your handler mapping. For this, register the handler for all extensions, using the path to the
Python interpreter and the path to the wfastcgi.py script:
EXTENSION

SCRIPT PROCESSOR PATH


ADDITIONAL ARGUMENTS

D:\python27\python.exe
D:\python27\scripts\wfastcgi.py

At this point, you can click the Save button at the bottom.
Finally, go back to the Dashboard. Go down to the SITE URL on the left hand side. Click the link and open your new Django
site, which will look like this:

Next Steps
From here, you can continue the development of your Django application by using the tools you are already using. If you are
using Python Tools for Visual Studio (http://pytools.codeplex.com/) for development, you will likely want to install VisualGit
(http://code.google.com/p/visualgit/) to get source control integration from within Visual Studio.
Your app may have dependencies beyond Python and Django. If you installed Python by using the installer at
http://www.windowsazure.com/en-us/develop/python/ (http://www.windowsazure.com/en-us/develop/python/), you will
already have PIP installed and can use this to quickly add new dependencies. For example, to install the Natural Language
toolkit and all of its dependencies, type:
pip install nltk
You then need to update the site-packages directory by copying the files from C:\Python27\Lib\site-packages into your local
site-packages directory.
After you copy the files, run the command git status to see the newly added files, and git add followed by git commit to
commit the changes to the repo. Finally, you can do a git push, which deploys the updated web site to Azure.
Now you can go into the DjangoApplication directory and use manage.py as you typically would to start adding new

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

applications to your
Django(/EN-US/ACCOUNT/)
project.
MY ACCOUNT

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

/en-us/gallery/)

8 of 10

(/en-us/pricing
Community
Support
/free-trial/)
Want to edit or suggest changes to this content? You can edit and submit changes
to this article

Documentation Contribute
(/en-us/documentation/)
to this

Downloads (/en-us/downloads/)
article

(https://github.com/WindowsAzure/azure-content/blob/master/articles/web-sites-python-create-deploy-djangoapp.md) using GitHub.

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL

19 Comments
Pricing

Documentation (/en-us/documentation/)

/en-us/gallery/)

9 of 10

Community

Downloads (/en-us/downloads/)

Support

Alessandro Lange D'Aquino

(/en-us/pricing
/free-trial/)

Sorry, but i'm kind of new on web dev. Isn't it necessary to have a web server like apache or nginx?Or
Azure does all that for you?
I'm in doubt if i use VS for a web app development and deployment or a old fashion but powerfull text/code
editor?

alanjds

Not intended to be rude here but... Dude, this tutorial is seriously weird to anyone used to develop with
Django! No pip mention, no virtualenv, calling django-admin.py by direct module?? Please mail anyone at
djangoproject.com and hire a huge tutorial cleanup.

Sascha Gottfried

Those guys are neither Django nor Pyramid experts. Same situation like 18 months ago. A lot
changed in PTVS since then and the python web frameworks addressed by PTVS as well. Did you
notice you can contribute to this article hosted as Markdown on GitHub? Go on!

Kazik117

I'm deploy application like in tutorial, but django couldn't find static files. How to configure settings to serve
with static files?

Sahas Katta

Avatar
Did you end-up finding a solution?

progger

Correct me if i'm wrong, but it seems like when this was written, there wars no Django Application in the
gallery, which is why a generic website was created in this tut. Now that there is, are all the environment
variables still needed? I have an existing Django app on my Mac and when I try to push it, it says my
deployment was successful but I still see nothing but the "it worked" page. any suggestions?

Jac Fitzgerald

After following this tutorial, I saw a 500 internal server error instead of the Django welcome page. Enabling
detailed error logs told me that the error was
Exception Type: ImportError at /
Exception Value: cannot import name sql
File "D:\home\site\wwwroot\site-packages\django\db\models\deletion.py" in module
5. from django.db.models import signals, sql
I've double checked all my settings variables, and it looks right. Any idea what could be wrong? Is the
version of Django in the install kit outdated or something? I saw a comment from one other user at
http://stackoverflow.com/quest... saying they also got a 500 internal server error after following this
tutorial, so hopefully it's not just me. In case it is, my code so far, although all auto-generated so not very
interesting, is at https://github.com/jacalata/op....

Jac Fitzgerald

fixed: found a rogue 'sql' entry in my gitignore file that was ignoring the whole relevant folder.

Zevendagenzonderzorgen

HELLLLPPPP

23/07/2014 13:34

Python Web Sites with Django - Azure tutorial

http://azure.microsoft.com/en-us/documentation/articles/web-sites-pyth...

MY ACCOUNT (/EN-US/ACCOUNT/)

PORTAL (HTTPS://MANAGE.WINDOWSAZURE.COM)

FREE TRIAL
Pricing

/en-us/gallery/)

10 of 10

Go Social

Microsoft Azure (/en-us/)

Community

Account

Features (/en-us/solutions/)

(http://go.microsoft.com/fwlink

(https://account.windowsazure.com)

Services (/en-us/services/)

/?linkid=394285&clcid=0x409)

(/en-us/pricing
Subscriptions
/free-trial/)

Regions (/en-us/regions/)

Blogs (/blog/)

(https://account.windowsazure.com

Case Studies (/en-us/case-

Service Updates (/en-us/updates/) /subscriptions/)

/fwlink/?linkid=306390&

studies/)

Forums (/en-us/support/forums/) Profile

clcid=0x409)

Pricing (/en-us/pricing/overview/)

Events (/en-us/community

(https://account.windowsazure.com

Calculator (/en-us/pricing

/events/)

/profile/)

Documentation (/en-us/documentation/)
Community
Facebook Support
(http://go.microsoft.com

Twitter
(http://go.microsoft.com
/fwlink/?linkid=306391&
clcid=0x409)

Rss

Downloads (/en-us/downloads/)

/calculator/)

Preview Features (/en-us/services

Documentation (/en-us

/preview/)

/documentation/)

Support (/en-us/support

Management Portal

Downloads (/en-us/downloads

/options/)

(https://manage.windowsazure.com/)

/?sdk=net)

Forums (/en-us/support/forums/)

Gallery (/en-us/gallery/)

Service Dashboard

Microsoft Azure in China

(http://status.azure.com)

Trust Center (/en-us/support

(http://windowsazure.cn/zh-cn/)

Support (/en-us/support

/trust-center/)

/options/)

Security (/en-us/support/trust-

(http://go.microsoft.com

center/security/)

/fwlink/?linkid=306392&

Privacy (/en-us/support/trustclcid=0x409)

center/privacy/)
Compliance (/en-us/support/trust-

Newsletter

center/compliance/)

(http://go.microsoft.com
/fwlink/?linkid=306393&
clcid=0x409)

Hello from Seattle.

English

(http://www.microsoft.com)

EUR

2014 Microsoft

Contact Us (http://support.microsoft.com/contactus/?ws=mscom)
Trademarks (http://go.microsoft.com/fwlink/p/?linkid=222682&clcid=0x409)
Privacy & Cookies (http://go.microsoft.com/fwlink/p/?linkid=131004&clcid=0x409) Feedback (http://feedback.azure.com/)

23/07/2014 13:34

S-ar putea să vă placă și