django azure python

Django on Azure- Learn to deploy and scale 100% free

Let's scale django app with azure.

Django on Azure- Learn to deploy and scale 100% free

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It’s known for its clean design and leaning towards rapid development cycles. Microsoft Azure is Microsoft’s cloud offering built to deploy and manage applications on its global network of data centers.

As I work with multi-cloud platform services with different technologies and tools, I will walk through deploying a data-driven python Django web app to azure app service via Azure CLI which is pretty awesome and connects it to an azure PostgreSQL database.

You can still use different methodologies to deploy your application.

Here is the architecture of our services, which we will be demonstrated. I’m not going to cover how to create or write a Django application, but instead, we’re going to focus on the last mile of development- getting it into production. But I will write about building a Simple API service in my upcoming blog articles. So stay updated.

# Prerequisites

I’m assuming that you already have:

  • Subscription to Azure
  • Knowledge and experience with Python and Django
  • Hands-on deploying web apps

If you are a student and don’t have an Azure subscription, no problem, you can have it through Azure for students.

Let’s get started. Well, Let’s talk about Azure App services. It has tons of features for everything. You don’t have to make your VM (Virtual Machine) Server and configure with Nginx and do all those stuff. I am not saying that VM is bad, and the Azure App service is cool.

Every service had its own pros and cons. You have to use those based on the requirements and conditions. I will be highlighting a few features of Azure App Services.

  • Scalability
  • Backup
  • SSL
  • Authentication and security
  • Many more

If You haven’t install Azure CLI, I prefer you to go through docs. Let’s check the Azure CLI version on our machine.

az --version

Let’s sign in to the Azure account through the Azure CLI.

az login

These commands gonna pop up in the browser to gather your credentials. The command may take a few minutes to complete. We will do the following things in the Azure cloud service.

  • Create a resource group and app service plan
  • Create an app with the specified name and PostgreSQL database
  • Zip deploy files from the current working directory to the app
  • Load testing Django App using Locust
  • Scale web app by Horizontal and Vertical one

Let’s create a resource group and app service plan.

az group create --name nameofresourcegroup --location westus2

Before creating the Postgres Database in Azure, make sure you had already installed the db-up extension as we will go through using Azure CLI.

az extension add --name db-up

Alright, let’s create the Postgres database in Azure with the az Postgres up command:

az postgres up --resource-group nameofresourcegroup --location westus2 --sku-name B_Gen5_1 --server-name nameofDBserver --database-name nameofyourdb --admin-user uradminuser --admin-password urpassword --sl-enforcement Enabled

After taking a few minutes, you will get outputs as a JSON object that contains credentials. Here we used B_Gen5_1 (Basic, Gen5, one core )pricing tier, the least expensive one. We have enabled access from your local IP Address and azure services. We have configured our resource-group with a nearby location with the Database server name, user, password and enforced the SSL for security purposes.

Let’s create the app service app with our service plan.

az webapp up --resource-group nameofrsgroup --location westus2 --plan appserviceplan -sku B1 --name nameofweb

We have just deployed our code to Azure app service with B1 (Basic pricing tier) that enables default logging and uploads the repo using zip deployment with build automation enabled. We choose a particular resource group, a nearby location that makes your services efficient with the service plan, and our web app’s unique name.
I hope your web app is live. But there are certain things you have to maintain.
These are simple CRUD API services made on Django. But we can’t request a method because we didn’t config the database part. Let’s configure environment variables to connect the database In azure app service; web app code expects to find DB information in fours environment viable named DBHOST, DBNAME, DB USER, DB PASS. To use production settings, it also needs the DJANGO_ENV environment variable set to production.

Your WSGI settings must be like this for production.

import os
from django.core.wsgi import get_wsgi_application

if os.environ.get('DJANGO_ENV') == 'production':

Make sure you had decoupled every credential in your Django project. Sample of database one. You can use Python-decouple too.

hostname = os.environ['DBHOST']

    'default' :{
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ['DBNAME'],
        'HOST': hostname + "",
        'USER': os.environ['DBUSER]+ '@'+hostname,
        'PASSWORD': os.environ['DBPASS']

I didn’t mention the PORT and SSL configuration in the database too. You can do it if it requires and faces any error for it.

'PORT': os.environ.get('DATABASE_PORT',''),
    'sslmode': 'require',

Let’s config using Azure CLI. You can also do the same thing using the Azure portal.

az webapp config appsettings set --settings DJANGO_ENV="production" DBHOST="" DBNAME="" DBUSER="" DBPASS=""

We have just config our environment variables. Now let’s migrate the Django database to Azure PostgreSQL. To get access, we will be using ssh.

az webapp ssh

You will be getting access to your server. Let’s do some command line stuff.

“`bash cd site/wwwroot source /antenv/bin/activate pip install -r requirements.txt python makemigrations python migrate python createsupueruser

Using SSH, we change the directory into www-root, where our code is located, and we activated our virtual environment. We have to install packages to migrate our database successfully. We migrated the database. Creating a superuser is optional, but it is better to look over our registered apps in Django.

Let's assume you need some updates or patches; you will have to do it on your local machine and directly push your azure app services. You can use az commands.

az webapp up

This command uses values that are cached locally in the .azure/config file, including the app name, resource group, and App service plan.

Congratulations! You’ve deployed your python app to App Service.

What about logging and looking out if a server goes down. We have a solution. You can use az command for it.

az webapp log tail

Now, let’s do scaling stuff. You can scale your services horizontally and vertically. I will not be explaining in this article much more about scaling. I will be writing upcoming articles. You can efficiently perform vertical scaling by adding more instances and loads. Ensure you maintain your instances and resources when your clients have a few requests if you are performing vertical scaling one. But that’s not the only solution. You can use horizontal scaling too.

We will be scaling our web app based on the request that our service consumes, and based on the metrics of the CPU consumption, we will add more instances. we configure based on the CPU percentage consumption, where our service will perform auto-scaling when the server consumes more requests and crossed the 60% CPU consumption. Let’s perform load testing using Locust. You can use k6 too. Let’s create a python file and use the below code.

from locust import HttpUser, between, task
import json

class WebsiteUser(HttpUser):
    wait_time = between(5,15)

    def index(self):
            data = json.dumps(
                    "title": "Learn load testing",
                    "description": "Learn loadtesting with locust",
            headers = {

The code will hit the payloads in URL /api/todo/v1/todo/create endpoints that will create a todo.

Use 8089 port and its address to browse in the browser.

Let’s give a few data and URLs.

We have hit many requests from more than 300 users in our endpoint to create a simple to-do. You can view more info through the chart too. We still have 0 % failure when we use the POST method in our endpoint.

Let’s look into the monitor in the Azure portal.

Well, as we configured with 60% CPU consumption on auto-scaling, we have reached up to 4.05% only. Thus, there will be no adding of the new instance. But this is how we perform auto-scaling on a different basis.

I hope this blog was helpful. Feel free to leave me feedback.

On this page

Category under

django azure python

Share this post

You might also like

Subscribe to new posts