Published in · 7 min read · Apr 23, 2019
--
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.
This will run the server on port 8000, and if successful, you’ll see the below screen when going tolocalhost:8000
in your browser:
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:
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
.
When you run your server, you should be seeing the following error:
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:
- [
git init
] Initialize your local repo as a git repo - [
git remote add origin
] Link your local repo with the remote repo on GitHub - [
git pull
] Pull the remote .gitignore GitHub created to your local repo - [
git add
] Add all files for staging - [
git commit
] Save changes to your local repo by “committing” them - [
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.
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!
Find the app service, underneath the resource created.
A screen similar to the following should pop up:
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.
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:
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.
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.
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
.
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!