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 .
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. (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 as well.
- A django project ofcourse (😆)
For this blog, I will be using my demo app, which is in this repository: github.com/notnotrachit/django_todolist_dem..
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
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
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.
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.
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:
Now we need to go to the deployment tab.
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.
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.
After the deployment has been done, you will see a page like this:
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.
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.
Congratulations 🎉, You have succesfully deployed your Django Web App to Azure. 🎉
Give yourself a pat on the back
I hope you find this blog helpful. Feel free to write any doubts in the comments.