This is a Python web app using the Django framework and the Azure Database for PostgreSQL relational database service. The Django app is hosted in a fully managed Azure App Service. This app is designed to be be run locally and then deployed to Azure. You can either deploy this project by following the tutorial Deploy a Python (Django or Flask) web app with PostgreSQL in Azure or by using the Azure Developer CLI (azd) according to the instructions below.
The requirements.txt has the following packages:
Package | Description |
---|---|
Django | Web application framework. |
pyscopg2-binary | PostgreSQL database adapter for Python. |
python-dotenv | Read key-value pairs from .env file and set them as environment variables. In this sample app, those variables describe how to connect to the database locally. This package is used in the manage.py file to load environment variables. |
whitenoise | Static file serving for WSGI applications, used in the deployed app. This package is used in the azureproject/production.py file, which configures production settings. |
This project is designed to work well with the Azure Developer CLI, which makes it easier to develop apps locally, deploy them to Azure, and monitor them.
This project has Dev Container support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension. If you're unable to open the Dev Container, then it's best to first create a Python virtual environment and activate that.
-
Install the requirements:
python3 -m pip install -r requirements.txt
-
Create an
.env
file using.env.sample
as a guide. Set the value ofDBNAME
to the name of an existing database in your local PostgreSQL instance. Set the values ofDBHOST
,DBUSER
, andDBPASS
as appropriate for your local PostgreSQL instance. If you're in the Dev Container, copy the values from.env.sample.devcontainer
. -
In the
.env
file, fill in a secret value forSECRET_KEY
. You can use this command to generate an appropriate value:python -c 'import secrets; print(secrets.token_hex())'
-
Run the migrations: (or use VS Code "Run" button and select "Migrate")
python3 manage.py migrate
-
Run the local server: (or use VS Code "Run" button and select "Run server")
python3 manage.py runserver
This repo is set up for deployment on Azure App Service (w/PostGreSQL server) using the configuration files in the infra
folder.
🎥 Watch a deployment of the code in this screencast.
Steps for deployment:
-
Sign up for a free Azure account
-
Install the Azure Dev CLI. (If you opened this repository in a Dev Container, that part will be done for you.)
-
Initialize a new
azd
environment:azd init
It will prompt you to provide a name (like "django-app") that will later be used in the name of the deployed resources.
-
Provision and deploy all the resources:
azd up
It will prompt you to login, pick a subscription, and provide a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location (like to "centralus") can help, as there may be availability constraints for some of the resources.
-
When
azd
has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the front page of the restaurant review app! 🎉 If you see an error, open the Azure Portal from the URL in the command output, navigate to the App Service, select Logstream, and check the logs for any errors. -
If you'd like to access
/admin
, you'll need a Django superuser. Navigate to the Azure Portal for the App Service, select SSH, and run this command:python3 manage.py createsuperuser
-
When you've made any changes to the app code, you can just run:
azd deploy
This project includes a Github workflow for deploying the resources to Azure on every push. That workflow requires several Azure-related authentication secrets to be stored as Github action secrets. To set that up, run:
azd pipeline config
The deployed resources include a Log Analytics workspace with an Application Insights dashboard to measure metrics like server response time.
To open that dashboard, just run:
azd monitor --overview
If you're working with this project and running into issues, please post in Issues.