-
Notifications
You must be signed in to change notification settings - Fork 672
Django and SQL Database 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 SQL database hosted on Azure, how to configure the application to use a SQL database, and how to publish the application to an Azure Website.
- Prerequisites
- Create the Project
- Create a SQL 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 an Azure SQL database.
You can create a database by following these steps.
-
Log into the Azure Management Portal.
-
At the bottom of the navigation pane, click NEW.
- Click DATA SERVICES, then SQL DATABASE, and then click QUICK CREATE.
-
Choose to create a New SQL database server.
-
Choose a Region/Affinity Group in which to locate the database. If you will be using the database from your Azure application, select the same region where you will deploy your application.
In this section, we'll configure our application to use the SQL 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 SQL databases with Django. Then we'll run the application locally.
-
In Azure Management Portal, click on SQL DATABASES, then click on the database you created earlier.
-
Click MANAGE.
- You will be prompted to update the firewall rules. Click YES. This will allow connections to the database server from your development machine.
-
Click on SQL DATABASES, then SERVERS. Click on the server for your database, then on CONFIGURE.
-
In this page, you'll see the IP address of every machine that is allowed to connect to the database server. You should see the IP address of your machine.
Below, under allowed services, make sure that Azure services are allowed access to the server. When the application is running in an Azure Website (which we'll do in the next section of this tutorial), it will be allowed to connect to the database. Click SAVE to apply the change.
-
In Visual Studio, open settings.py, from the ProjectName folder. Edit the definition of
DATABASES
.DATABASES = { 'default': { 'ENGINE': 'sql_server.pyodbc', 'NAME': '<DatabaseName>', 'USER': '<User>@<ServerName>', 'PASSWORD': '<Password>', 'HOST': '<ServerName>.database.windows.net', 'PORT': '<ServerPort>', 'OPTIONS': { 'driver': 'SQL Server Native Client 11.0', 'MARS_Connection': 'True', } } }
<DatabaseName>
,<User>
and<Password>
are the values you specified when you created the database and server.The values for
<ServerName>
and<ServerPort>
are generated by Azure when the server is created, and can be found under the Connect to your database section. -
In Solution Explorer, under Python Environments, right-click on the virtual environment and select Install Python Package.
-
Install the package
pyodbc
using easy_install.
- Install the package
django-pyodbc-azure
using pip.
-
Right-click the project node and select Python, Django Sync DB.
This will create the tables for the SQL 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 SQL 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 SQL 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.