Skip to content

Django and MySQL on Azure

crwilcox edited this page Apr 23, 2015 · 2 revisions

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.

Django and MySQL on Azure video

Prerequisites

Create the Project

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.

  1. In Visual Studio, select File, New Project.

  2. 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.

New Project

  1. You will be prompted to install external packages. Select Install into a virtual environment.

External Packages

  1. Select Python 2.7 as the base interpreter.

Add Virtual Env

  1. Right-click the project node and select Python, Django Sync DB.

Django Console

  1. This will open a Django Management Console. Follow the prompts to create a user.

    This will create a sqlite database in the project folder.

Django Console

  1. Confirm that the application works by pressing F5.

  2. Click Log in from the navigation bar at the top.

Web Browser

  1. Enter the credentials for the user you created when you synchronized the database.

Web Browser

  1. Click Create Sample Polls.

Web Browser

  1. Click on a poll and vote.

Web Browser

Create a MySQL Database

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.

  1. Log into the Azure Management Portal.

  2. At the bottom of the navigation pane, click NEW.

New

  1. Click STORE, then ClearDB MySQL Database.

ClearDB Step 1

  1. In Name, type a name to use for the database service.

  2. 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.

ClearDB Step 2

  1. Click PURCHASE.

Configure the Project

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.

  1. In Azure Management Portal, click on ADD-ONS, then click on the ClearDB MySQL Database service you created earlier.

  2. Click on CONNECTION INFO. You can use the copy button to put the value of CONNECTIONSTRING on the clipboard.

Manage Access Keys

  1. 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': '',
        }
    }
  2. In Solution Explorer, under Python Environments, right-click on the virtual environment and select Install Python Package.

  3. Install the package mysql-python using easy_install.

Install Package

  1. 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.

  2. 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.

Publish to an Azure Website

PTVS provides an easy way to deploy your web application to an Azure Website.

  1. In Solution Explorer, right-click on the project node and select Publish.

Publish

  1. Click on Microsoft Azure Websites.

  2. Click on New to create a new site.

  3. Select a Site name and a Region and click Create.

Create Web Site

  1. Accept all other defaults and click Publish.

  2. 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!

Web Browser

Clone this wiki locally