Deploy Django to Azure - The Easy Way
Tags: webdev, django, cloud, azure
Published: 2024-07-11 12:21:55

The general perception is that deploying applications on the cloud is complicated, but that is not always true. We have really easy ways to deploy stuff on the cloud as well. So I will be demonstrating how to deploy Django Webpps to Azure easily. 

### Prerequisites

- Azure account

If you don’t have an azure account yet, no issues, you can signup for free and get 200USD credits for the first month [here](https://azure.microsoft.com/free/?wt.mc_id=studentamb_285271) . 

If you are a student and don’t have a credit card, no worries. You can sign up for Azure for Students and get $100 credit for free using your student email ID [here](https://azure.microsoft.com/free/students/?wt.mc_id=studentamb_285271). (Can be renewed annually as long as you are a student)

- Hosted database

If you have a web app on Django that is dependent on a database, make sure it is using a hosted database instead of the default SQLite database. The hosted database can be anywhere, be it Azure itself, or you can use [neon.tech](http://neon.tech) as well. 

- A django project ofcourse (😆)

For this blog, I will be using my demo app, which is in this repository: https://github.com/notnotrachit/django_todolist_demo_sample

Keep in mind that my sample app is not dependent on any database, its just using the client’s localstorage, if your app is dependent on the database then it is highly recommended to first add a hosted database and configure it accordingly. 
You can refer to django’s docs for the same: [https://docs.djangoproject.com/en/5.0/ref/databases](https://docs.djangoproject.com/en/5.0/ref/databases)

If you are still facing issues, kindly let me know, I will make another blog regarding the same. 

## Lets Deploy

### 1) Make a GitHub repository

Make sure you make a GitHub repository and make sure you push all your code to that repository. The repository can be public or private. 

### 2) Head over to Azure Portal

Go to the azure portal’s home page at:  [https://portal.azure.com/#home](https://portal.azure.com/#home?wt.mc_id=studentamb_285271)


![Azure Portal Homescreen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbfa7vsx7xn7w0z6hdpt.png)

You might see a different kind of homepage. Since I already have many things deployed, it shows up differently for me, but don’t worry—the process remains the same. 

### 3) Creating an App Service

We will be deploying our webapp using Azure App Service. Azure App service is a really amazing service that we can use to very easily deploy our webapps to the cloud without worrying or configuring any infrastructure. It handles that automatically. 

To create a service, search for 'App Service' in the top search bar and click on it. 


![Azure search bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52gd0xvu5ety3slvz6ir.png)


![Azure app service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/am3rotexth6gydobq0kz.png)

You might see an empty list. 

Now you need to click on `Create` and then select `Web App` 

After that a long form type page will open up.


![Create app service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ksdamg88mhvg7mrrc4d.png)

The first field is for the subscription. It will most likely be prefilled. If not, select your subscription from the dropdown.
 
Next is the resource group. You can leave it for now; it will be automatically created once we assign a name.
 
Now, we need to set a name for our web app. This name needs to be globally unique. 
In publish option, leave code as selected. 

In the Runtime Stack, select Python 3.10 or higher
Next you can select the region or leave it at the default as well. 

Next, you will see under Pricing plan that a new Linux plan has already been created for you. 
You can now select the pricing plan according to your needs. I am selecting Basic B1 for now. 

Your final page will look something like this:


![Create app service final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5uemn4gyhxzt40xzo1ua.png)

Now we need to go to the deployment tab. 


![Deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpi93fxnrq7njcr43vnq.png)

Here you first need to enable Continuous deployment. 
After enabling that, you need to Authorize your GitHub account by logging into your account. 

Once you authorize that, you need to select the repository and branch.


![Deployment tab final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju42b42r0ipcrskulrze.png)

Now click on `Review + create` . 
You will be taken to the final page where you can review everything. 
Finally, you can click on `Create` now. 

Now it will takes few minutes to deploy your application on the cloud. 

![Deployment in process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgusyopxpvnofprifs88.png)

After the deployment has been done, you will see a page like this:

![Deployment done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uhw5i3vopnqo4bd4sgd.png)

Now click on `Go to resource` . 
It will take few seconds to load. After all data has been loaded, you will be able to see the domain. 


![Resource Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3kk7uy4415cp6kbczzj.png)

You can try visiting it, but you will be greeted with an error. 
Don’t worry, we will solve that as well. 

The error is mainly because we haven’t really allowed our application to run on this domain. 
To allow that, we would need to add this domain in our settings file. 

### 4) Making Required Changes

We will now go to our editor and open the `settings.py` file. 
Find `ALLOWED_HOSTS` in that file. By default, it will be an empty list. Or in some cases there might be previously added domains as well. Don’t worry. 
Now, we need to add our domain in that list. 

Kinda like this: `ALLOWED_HOSTS = ["rachit-todo.azurewebsites.net"]` 
But make sure you enter your webapp’s domain. 

Once done, save the file and commit it. Then once you push the commit to GitHub, Azure will automatically start deploying the webapp again. It will take few minutes to deploy.


![GitHub Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3bferxxot1khdm988mk.png)

Congratulations 🎉, You have succesfully deployed your Django Web App to Azure. 🎉

Give yourself a pat on the back 

![https://i.imgur.com/brtcekW.gif](https://i.imgur.com/brtcekW.gif)

I hope you find this blog helpful. 
Feel free to write any doubts in the comments.