Deploying a Basic Django App using Azure App Services (2024)

Django is a commonly used web framework based on Python, used and loved by developers around the world. But, wouldn’t it be cool to share a website you made using Django with the people around you? Microsoft Azure, one of the biggest cloud providers in the market, offers a neat feature called Azure App Services, allowing you to deploy web apps live.

Follow this guide to deploy your very own Django web app, from start to finish. Completed code can be found here: https://github.com/selynna/django-azure-demo.

First, ensure that you have Python 3.5+ installed. You can download the latest version (3.7.3) here: https://www.python.org/downloads/. Once that’s installed, you’ll have Python’s package manager, pip, included by default. Through pip, we’ll want to add a package called virtualenv, by running the command pip install virtualenv(or if you have Python 2 by default, use pip3 instead of pip.

Using the virtualenv package, we’re now going to create a virtual environment called deploydjango for our Django project, which allows us to isolate our setup from the actual system itself. Create one using the command python -m venv deploydjango (or if you have Python 2 by default, use python3 instead of python). This will create a virtual environment in the latest Python 3 installation you have. To activate the virtual environment, use the command source deploydjango/bin/activate. Now, your terminal should prefix your commands with (deploydjango), and will look something like this:(deploydjango) ssun ~

You can read more about virtual environments here: https://virtualenv.pypa.io.

Now that the virtual environment is set up, we’re ready to start setting up our Django project itself! We haven’t installed Django yet, so let’s do that by running pip install django. Note: if pip isn’t installed, make sure that you have Python properly installed (per the previous section).

After that installs successfully, it’ll allow us access to Django subfunctions. To create the initial files for the project, you can run django-admin startproject <folder_name>. I’ll call my folderdjango_azure_demo.

File Structure

Now, to see the folder created, running ls on Mac or dir on Windows should show you the newly created django_azure_demo folder. Afterwards, run cd django_azure_demo to see the files created. Out of the box, the folder should contain db.sqlite3, the django_azure_demo/ folder, and manage.py.

Your file structure should look something like this:

Running your Django server

Once the django-admin command is run, you’re able to immediately run your Django server by typing python manage.py runserver in your terminal. If it’s successful, you should see the below terminal output pop up.

Deploying a Basic Django App using Azure App Services (3)

This will run the server on port 8000, and if successful, you’ll see the below screen when going tolocalhost:8000 in your browser:

Deploying a Basic Django App using Azure App Services (4)

Great! The project setup is good to go, so now it’s time to create the actual Django app! We’ll use the manage.py file to create an app called Food (by creating a subdirectory in the root project folder), by running python manage.py startapp food. If you cd into food, you should see this file structure:

Deploying a Basic Django App using Azure App Services (5)

You might be wondering, what’s a view? According to the Django website, views are Python functions or classes that take a web request and return a web response. In short, this is how our website will be displayed using a view.

Ready to get started?

In food/views.py, replace the code currently in it with the code below:

In order to use the view, we need to link it to a URL. Create food/urls.py and put the following code in:

Next, go to django_azure_demo/urls.py and replace the code with the following:

We just created an index page for our food app, which you can access at localhost:8000

…after one last additional step. We haven’t wired the food app to our main project. Go into django_azure_demo/settings.py and under the INSTALLED_APPS array, add ‘food’,.

Now, we should be able to see the following page when accessing localhost:8000.

Deploying a Basic Django App using Azure App Services (6)

When you run your server, you should be seeing the following error:

Deploying a Basic Django App using Azure App Services (7)

You can fix this by doing python manage.py migrate. To see the migrations performed, you can do python manage.py makemigrations. The purpose of migrate is to propagate any changes made to the database.

Read more here: https://docs.djangoproject.com/en/2.2/topics/migrations/

Now that we have it running locally, let’s deploy this app onto Azure.

Pushing our code to GitHub

First, we’ll push all of our code to a GitHub repo for the project. If you don’t have an account already, create one at https://github.com. Once you have an account, create a new repository (I’ll call mine django-azure-demo), and without the “initialize with a README.md” checked, and adding a .gitignore for Python. In your terminal, go to your root project folder and run these commands:

$ git init
$ git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO
$ git pull origin master
$ git add -A
$ git commit -m “Initial commit”
$ git push origin master

This will:

  1. [git init] Initialize your local repo as a git repo
  2. [git remote add origin] Link your local repo with the remote repo on GitHub
  3. [git pull] Pull the remote .gitignore GitHub created to your local repo
  4. [git add] Add all files for staging
  5. [git commit] Save changes to your local repo by “committing” them
  6. [git push] Pushing them to GitHub. You can view your files at your GitHub repo, accessible at https://github.com/YOUR_USERNAME/django-azure-demo.

Find more information on pushing to GitHub here.

Initializing Azure

Let’s initialize the Azure portion. If you don’t already have Azure, create a free account at azure.com. Upon account creation, you’ll have a subscription with $200 of credit for the first 30 days, with services accessible in your Azure portal at portal.azure.com. If you’re a student, get access to Azure for Students here.

Our Django app will be deployed through Azure’s web app service. To start off, we’ll create a resource from the upper left “Create a Resource” button. Click on the “Web App” button, but if it isn’t there, you can search for “Web App” in the search bar.

Deploying a Basic Django App using Azure App Services (8)

Fill out the information required. I’ll be calling my app azure-django-demo. Make sure that when you’re filling the configuration out, that you pick Linux as your operating system, not Windows!

Deploying a Basic Django App using Azure App Services (9)

Find the app service, underneath the resource created.

Deploying a Basic Django App using Azure App Services (10)

A screen similar to the following should pop up:

Deploying a Basic Django App using Azure App Services (11)

Click on “Deployment Center” in the left sidebar (the light sidebar, not the dark). A screen titled “Deployment Center” with 3 steps displayed should pop up. You’ll first 1) deploy through GitHub, 2) use the App Service build service, then 3) configure with your username, repo, and branch.

Deploying a Basic Django App using Azure App Services (12)

Finally, we’ll make a few configuration changes to our Python app. In the search bar, find “Extensions”. You’ll get a screen like this:

Deploying a Basic Django App using Azure App Services (13)

Scroll down to find and click the latest Python version (currently at Python 3.7). Once that’s added, we’ll use the search bar to search for “Configuration”. Go into “General settings” in the window that pops up, and change the Stack and Python Version to Python and 3.7, respectively.

Deploying a Basic Django App using Azure App Services (14)

You should now be able to access your website at azure-django-demo.azurewebsites.net. However, it should still throw an error — there’s a few things we’re missing.

Azure deployment files

In order to deploy onto Azure, we’ll need one additional file, requirements.txt,which specifies the packages that Azure needs to install for your app to work. Copy the below file into your root project folder.

One more thing — did you notice the ALLOWED_HOSTS array in settings.py? We can’t access our Azure app live if the Azure URL generated isn’t in that array. An easy workaround we’ll use for now is to add ‘*’, like this: ALLOWED_HOSTS = [‘*’]. Ultimately, we’ll want to restrict this to the specific domain for security purposes.

Push all files and changes to your GitHub repo, by running:

$ git add -A
$ git commit -m “Add Azure deployment files"
$ git push origin master

After changes have been pushed, we can view our deployment progress in “Deployment Center” of the app service.

Deploying a Basic Django App using Azure App Services (15)

In the far right column labeled “Logs”, click on the blue book button, and scroll to get to the rightmost part of the window that pops up. Clicking “Show Logs” will show you the different processes happening behind the scenes (for example, pip installing Django through requirements.txt).

Once it’s successful, you should be able to see the app you created at your Azure link, <your_azure_project>.azurewebsites.net.

Deploying a Basic Django App using Azure App Services (16)

Congrats! You just deployed your Django app using Azure App Services 🎉

Interested in learning how to style your Django app or use an SQLite database to store and display data? Look out for an update soon!

Deploying a Basic Django App using Azure App Services (2024)
Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5892

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.