-
Notifications
You must be signed in to change notification settings - Fork 676
Django and MySQL on Azure
In this tutorial, we'll create a simple polls application using one of the PTVS sample templates.
We'll learn how to use a MySQL service hosted on Azure, how to configure the application to use MySQL, and how to publish the application to an Azure Website.
- Prerequisites
- Create the Project
- Create a MySQL Database
- Configure the Project
- Publish to an Azure Website
- Visual Studio 2012 or 2013
- PTVS 2.1
- PTVS 2.1 Samples VSIX
- Azure SDK Tools for VS 2013 or Azure SDK Tools for VS 2012
- Python 2.7 32-bit
In this section, we'll create a Visual Studio project using a sample template. We'll create a virtual environment and install required packages. We'll create a local database using sqlite. Then we'll run the application locally.
-
In Visual Studio, select File, New Project.
-
The project templates from the PTVS Samples VSIX are available under Python, Samples. Select Polls Django Web Project and click OK to create the project.
- You will be prompted to install external packages. Select Install into a virtual environment.
- Select Python 2.7 as the base interpreter.
- Right-click the project node and select Python, Django Sync DB.
-
This will open a Django Management Console. Follow the prompts to create a user.
This will create a sqlite database in the project folder.
-
Confirm that the application works by pressing F5.
-
Click Log in from the navigation bar at the top.
- Enter the credentials for the user you created when you synchronized the database.
- Click Create Sample Polls.
- Click on a poll and vote.
For the database, we'll create a ClearDB MySQL hosted database on Azure.
As an alternative, you can create your own Virtual Machine running in Azure, then install and administer MySQL yourself.
You can create a database with a free plan by following these steps.
-
Log into the Azure Management Portal.
-
At the bottom of the navigation pane, click NEW.
- Click STORE, then ClearDB MySQL Database.
-
In Name, type a name to use for the database service.
-
Choose a Region/Affinity Group in which to locate the database service. If you will be using the database from your Azure application, select the same region where you will deploy your application.
- Click PURCHASE.
In this section, we'll configure our application to use the MySQL database we just created. We'll see how to obtain connection settings from the Azure portal. We'll also install additional Python packages required to use MySQL databases with Django. Then we'll run the application locally.
-
In Azure Management Portal, click on ADD-ONS, then click on the ClearDB MySQL Database service you created earlier.
-
Click on CONNECTION INFO. You can use the copy button to put the value of CONNECTIONSTRING on the clipboard.
-
In Visual Studio, open settings.py, from the ProjectName folder. Temporarily paste the connection string in the editor. The connection string is in this format:
Database=<NAME>;Data Source=<HOST>;User Id=<USER>;Password=<PASSWORD>
Change the default database ENGINE to use MySQL, and set the values for NAME, USER, PASSWORD and HOST from the CONNECTIONSTRING.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '<Database>', 'USER': '<User Id>', 'PASSWORD': '<Password>', 'HOST': '<Data Source>', 'PORT': '', } }
-
In Solution Explorer, under Python Environments, right-click on the virtual environment and select Install Python Package.
-
Install the package
mysql-python
using easy_install.
-
Right-click the project node and select Python, Django Sync DB.
This will create the tables for the MySQL database we created in the previous section. Follow the prompts to create a user, which doesn't have to match the user in the sqlite database created in the first section.
-
Run the application with F5. Polls that are created with Create Sample Polls and the data submitted by voting will be serialized in the MySQL database.
PTVS provides an easy way to deploy your web application to an Azure Website.
- In Solution Explorer, right-click on the project node and select Publish.
-
Click on Microsoft Azure Websites.
-
Click on New to create a new site.
-
Select a Site name and a Region and click Create.
-
Accept all other defaults and click Publish.
-
Your web browser will open automatically to the published site. You should see the application working as expected, using the MySQL database hosted on Azure.
Congratulations!
- Documentation on docs.microsoft.com
- PTVS Project
- Development topics
- Additional resources
- wfastcgi (on PyPI)
- Video index; note that some videos are outdated.