Coder Social home page Coder Social logo

jsusnik / msdocs-django-postgresql-sample-app Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/msdocs-django-postgresql-sample-app

0.0 0.0 0.0 2.52 MB

A sample Django app using PostgreSQL for the Azure App Service Web App + Database tutorial

License: MIT License

Shell 0.24% JavaScript 0.23% Python 19.40% HTML 16.90% Dockerfile 0.36% Bicep 62.88%

msdocs-django-postgresql-sample-app's Introduction

Deploy a Python (Django) web app with PostgreSQL in Azure

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.

Requirements

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.

Using this project with the Azure Developer CLI (azd)

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.

Local development

This project has devcontainer 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 devcontainer, then it's best to first create a Python virtual environment and activate that.

  1. Install the requirements:
pip install -r requirements.txt
  1. Create an .env file using .env.sample as a guide. Set the value of DBNAME to the name of an existing database in your local PostgreSQL instance. Set the values of DBHOST, DBUSER, and DBPASS as appropriate for your local PostgreSQL instance. If you're in the devcontainer, copy the values from .env.sample.devcontainer.

  2. Run the migrations: (or use VS Code "Run" button and select "Migrate")

python manage.py migrate
  1. Run the local server: (or use VS Code "Run" button and select "Run server")
python manage.py runserver

Deployment

This repo is set up for deployment on Azure App Service (w/PostGreSQL server) using the configuration files in the infra folder.

๐ŸŽฅ Watch me deploy the code in this screencast.

Steps for deployment:

  1. Sign up for a free Azure account
  2. Install the Azure Dev CLI. (If you opened this repository in a devcontainer, that part will be done for you.)
  3. Provision and deploy all the resources:
azd up

It will prompt you to login and to provide a name (like "django-app") and 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 are availability constraints for some of the resources.

  1. 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.

Screenshot of Django restaurants website

  1. 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:
python manage.py createsuperuser
  1. When you've made any changes to the app code, you can just run:
azd deploy

CI/CD pipeline

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

Monitoring

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

Getting help

If you're working with this project and running into issues, please post in Discussions.

msdocs-django-postgresql-sample-app's People

Contributors

pamelafox avatar vmagelo avatar davidcberry13 avatar microsoftopensource avatar jsusnik avatar microsoft-github-operations[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.