{"id":19099818,"url":"https://github.com/Azure-Samples/msdocs-python-django-azure-container-apps","last_synced_at":"2025-04-18T17:32:07.405Z","repository":{"id":61175575,"uuid":"548942511","full_name":"Azure-Samples/msdocs-python-django-azure-container-apps","owner":"Azure-Samples","description":"Python web app using Django that can be deployed to Azure Container Apps.","archived":false,"fork":false,"pushed_at":"2023-12-12T19:51:10.000Z","size":118,"stargazers_count":11,"open_issues_count":0,"forks_count":38,"subscribers_count":16,"default_branch":"main","last_synced_at":"2023-12-12T20:44:35.262Z","etag":null,"topics":["azure","container-apps","django","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Azure-Samples.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-10-10T12:32:27.000Z","updated_at":"2023-12-08T00:50:15.000Z","dependencies_parsed_at":"2023-12-12T20:38:48.286Z","dependency_job_id":"9cacb7fc-4b88-42d5-be90-018e41b5fbcf","html_url":"https://github.com/Azure-Samples/msdocs-python-django-azure-container-apps","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fmsdocs-python-django-azure-container-apps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fmsdocs-python-django-azure-container-apps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fmsdocs-python-django-azure-container-apps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fmsdocs-python-django-azure-container-apps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Azure-Samples","download_url":"https://codeload.github.com/Azure-Samples/msdocs-python-django-azure-container-apps/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783108,"owners_count":17201903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azure","container-apps","django","python"],"created_at":"2024-11-09T03:52:18.519Z","updated_at":"2025-04-18T17:32:07.399Z","avatar_url":"https://github.com/Azure-Samples.png","language":"Python","funding_links":[],"categories":["python","azure"],"sub_categories":[],"readme":"# Deploy a Python (Django) app to Azure to Azure Containers Apps\n\nThis Python app is a simple restaurant review application built with the [Django](https://www.djangoproject.com/) framework. The app stores application data in PostgreSQL with environment variables defining the connection info.\n\nThis repo was created to be built to a Docker image and run as a container instance in [Azure Container Apps](https://azure.microsoft.com/services/container-apps/). For more information, see the tutorial [Deploy a Python web app on Azure Container Apps with GitHub Actions](https://learn.microsoft.com/azure/developer/python/tutorial-deploy-python-web-app-azure-container-apps-01).\n\nThis Python web app repo can also be used in other ways:\n\n* You can run the web app locally in a virtual environment. Make sure to define *.env* file with environment settings.\n\n  * In the `.env` file, fill in a secret value for `SECRET_KEY`. You can use this command to generate an appropriate value for your product deployment:\n\n    ```shell\n    python -c 'import secrets; print(secrets.token_hex())'\n    ```\n\n* You can create a container locally and run it in Docker locally. You need Docker Desktop installed. For this scenario, set REMOTE_POSTGRESQL=1 and other environment variables in a *.env* file to point to a PostgreSQL server instance in Azure. You also need to create an application service principal, add it as a PostgreSQL ROLE to the server, and grant it necessary permissions on the database. See the *.env.example* file for details.\n\n  ```bash\n  docker build --file Dockerfile --tag pythoncontainer:latest .\n  docker run -it --env-file .env --publish 8000:8000/tcp pythoncontainer:latest\n  ```\n\n  If you want to use PostgreSQL instance locally, you add `--add-host` to the Docker command. For more information, see the [Docker run](https://docs.docker.com/engine/reference/commandline/run/) command. For an example of how to do this with MongoDB, see [Build and test a containerized Python web app locally](https://docs.microsoft.com/azure/developer/python/tutorial-containerize-deploy-python-web-app-azure-02).\n\n* You can deploy the code (not a container) to App Service. For guidance on how to deploy code, see [Quickstart: Deploy a Python (Django or Flask) web app to Azure App Service](https://docs.microsoft.com/azure/app-service/quickstart-python) and [Overview: Deploy a Python web app to Azure with managed identity](https://docs.microsoft.com/azure/developer/python/tutorial-python-managed-identity-01).\n\n* You can create a Docker image from this repo and host the container image in Web Apps for Containers (App Service). See [Containerized Python web app on Azure](https://docs.microsoft.com/azure/developer/python/tutorial-containerize-deploy-python-web-app-azure-01).\n\nIf you need an Azure account, you can [create on for free](https://azure.microsoft.com/free/).\n\nA Flask sample application with similar functionality is at https://github.com/Azure-Samples/msdocs-python-flask-azure-container-apps.\n\n## Requirements\n\nThe [requirements.txt](./requirements.txt) has the following packages:\n\n| Package | Description |\n| ------- | ----------- |\n| [Django](https://pypi.org/project/Django/) | Web application framework. |\n| [pyscopg2-binary](https://pypi.org/project/psycopg-binary/) | PostgreSQL database adapter for Python. |\n| [gunicorn](https://pypi.org/project/gunicorn/) | WSGI HTTP Server for UNIX. Required for running containers in [VS Code](https://code.visualstudio.com/docs/containers/quickstart-python#_gunicorn-modifications-for-djangoflask-apps).|\n| [python-dotenv](https://pypi.org/project/python-dotenv/) | Read key-value pairs from .env file and set them as environment variables. In this sample app, environment variables describe how to connect to the database and storage resources. Because managed identity is used no sensitive information is included in environment variables. \u003cbr\u003e\u003cbr\u003e This package is used in the [manage.py](./manage.py) file to load environment variables. |\n| [requests](https://pypi.org/project/requests/) | Python HTTP for Humans. |\n| [whitenoise](https://pypi.org/project/whitenoise/) | Static file serving for WSGI applications, used in the deployed app. \u003cbr\u003e\u003cbr\u003e This package is used in the [azureproject/production.py](./azureproject/production.py) file, which configures production settings. |\n\n## Deploying to Azure Container Apps\n\nThe steps to do this are covered more completely in the tutorial [Deploy a Python web app on Azure Container Apps with GitHub Actions](https://learn.microsoft.com/azure/developer/python/tutorial-deploy-python-web-app-azure-container-apps-01). Briefly, here are the steps:\n\n1. Fork and then clone locally.\n1. Create an Azure Container Registry.\n1. Build a container image from the repo.\n1. Create an Azure Database for PostgreSQL Flexible Server instance.\n1. Make your Azure user account a Microsoft Entra administrator on the server.\n1. Create a database on the server.\n1. Create a user-assigned managed identity.\n1. Add the managed identity as a ROLE on the PostgreSQL server.\n1. Assign the managed identity PostgreSQL permissions on the database.\n1. Deploy the web app container to Azure Container Apps.\n1. Migrate and create database schema by connecting to the container app and issuing the `python manage.py migrate` command.\n1. Configure continuous deployment.\n\nSee the [create_resources.sh](./create_resources.sh) Bash script for automating the creation of these steps. For best results, run the script in Azure Cloud Shell.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Fmsdocs-python-django-azure-container-apps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAzure-Samples%2Fmsdocs-python-django-azure-container-apps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Fmsdocs-python-django-azure-container-apps/lists"}