Deploying Django Rest API on Azure

My first experience on deploying Django to Azure

Yubaraj Singh
3 min readJun 21, 2021
credit:Unsplash

So recently my build API based on the Django Rest framework. I was not quite familiar with Django even though I could set up the Django code and it needed to be deployed to the server. I Recently had claimed a Github student benefit pack and I claimed $100 credit for the Azure student account.

So I login to the account and started my deployment process by creating an App service. So here are the things that were need for creating an app service.

  • runtime stack: python 3.7

I use a free service that serves as the site’s requirement

The project of Django was already uploaded in the Github so after creating the app service, it was ready for deployment. In the app, service left sidebar after scrolling there is a deployment section. I choose Github as a source so you may choose where you have uploaded it.

After this my site was up and running…

API fetching problem(cors)

I was trying to fetch API using Javascript and I wasn’t receiving any data.

fetch('https://vsvapp.azurewebsites.net/').then(resp=>resp.json()).then(data=>console.log(data)).catch(err=>{console.log(err)})

output: TypeError: Failed to fetch

solution:

This was because of CORS. Cross-Origin Resource Sharing (CORS) allows JavaScript code running in a browser on an external host to interact with your backend. So In the API section of the App service, there is CORS where we need to enable CORS which give permission to allow resource sharing. To allow all the path we need to assign asterisk(*) to the path.

This is how I was able to access the API and use it for another project.

SQLite trouble

Everything was running smoothly and the data in API were increasing but on a fine morning when I checked the data, everything was gone. Only the default data were available. This was a serious issue and I didn't know what was going on. SQLite saves data in the storage. And that was causing the problem in the Azure cloud. Azure saves the data temporarily as there wasn't any medium for saving the API to any storage form and I came to this StackOverflow section but still this didn't help as I was too naive to understand the Azure storage system even though I tried mounting shared storage to the app services.

So I had to give up the SQLite database and I migrated to MySQL database. This was another trouble but there were plenty of tutorials and guideline. In addition, Azure provides a dedicated MySQL database.

So this was my first experience while hosting Django restful onto Azure cloud.

--

--

Yubaraj Singh

I am Tech enthusiast, movie lover and admirer of Art and Innovations.