Build a Web App using Django

Let's build a web app using Django & Postgres, a mature full stack setup.

Requirements

Setup

Open the Terminal and create a python virtual environment

python3 -m venv ~/.venvs/appenv
source ~/.venvs/appenv/bin/activate

Install phidata

pip install phidata

Create your codebase

Create your codebase using the django-app template.

phi ws create -t django-app -n django-app

This will create a folder named django-app with the following structure:

django-app
├── app               # directory for django files
├── nginx             # directory for nginx used in production for static files
├── manage.py         # django-admin file
├── Dockerfile        # Dockerfile for the application
├── pyproject.toml    # python project definition
├── requirements.txt  # python dependencies generated by pyproject.toml
├── scripts           # directory for helper scripts
├── tests             # directory for unit tests
└── workspace
    ├── dev_resources.py  # Dev resources running locally
    ├── prd_resources.py  # Production resources running on AWS
    ├── secrets           # directory for storing secrets
    └── settings.py       # Phidata workspace settings

Run Django locally

The Django App comes with a Django server connected to a Postgres database. Run it using:

phi ws up dev:docker

Press Enter to confirm and give a few minutes for the image to download (only the first time). Verify container status and view logs on the docker dashboard.

View the Django App

  • Open localhost:8000 to view the Django App running locally.
  • Checkout the app folder for the django files.
  • DjangoApp resources are defined in the workspace/dev_resources.py file.
django-app-django-local

View Database

  • Connect to the postgres database on port 9315, schema: dev, user: app, password: app.
  • Credentials are defined in the workspace/secrets/dev_db_secrets.yml file.
  • PosgresDb resources are defined in the workspace/dev_resources.py file.
django-app-database-local

Database Migrations

Setting the MIGRATE_DB env var for the DjangoApp in workspace/dev_resources.py will run python manage.py migrate in the entrypoint.sh script.

workspace/dev_resources.py

# -*- DjangoApp running on port 8000
dev_django = DjangoApp(
    ...
    env={
        ...
        # Migrate database on startup using python manage.py migrate in entrypoint.sh
        "MIGRATE_DB": ws_settings.dev_db_enabled,
    },
    ...
)

Delete local resources

Stop the workspace using:

phi ws down dev:docker

Run Django on AWS

Now let's run the Django App in production on AWS.

AWS Authentication

To run on AWS, you need one of the following:

  1. The ~/.aws/credentials file with your AWS credentials
  2. AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY environment variables

Add AWS Region and Subnets

Add 2 Subnets to the workspace/settings.py file, these are required to create ECS resources.

workspace/settings.py

ws_settings = WorkspaceSettings(
    ...
    # -*- AWS settings
    # Region for AWS resources
    aws_region="us-east-2",
    # Availability Zones for AWS resources
    aws_az1="us-east-2a",
    aws_az2="us-east-2b",
    # Subnet IDs in the aws_region
    subnet_ids=["subnet-0xxa", "subnet-0xxb"],
    ...

Run Django App on AWS

Create AWS resources for the Django App using:

phi ws up prd:aws

This will create:

  1. ECS Cluster for running the application.
  2. ECS Task Definition for the application.
  3. ECS Service that run the tasks on the ECS cluster.
  4. LoadBalancer to route traffic to the application.
  5. Security Groups that control incoming and outgoing traffic.
  6. RDS Database for running Postgres.
  7. Secrets for managing application and database secrets.

Press Enter to confirm and give a few minutes for the resources to spin up.

  • These resources are defined in the workspace/prd_resources.py file.
  • Use the ECS console to view services and logs.
  • Use the RDS console to view the database instance.

View the Django app

  • Open the LoadBalancer DNS to view your Django App running on AWS.
django-app-django-prd

Database Migrations

Setting the MIGRATE_DB env variable for the DjangoApp in workspace/prd_resources.py will run python manage.py migrate in the entrypoint.sh script.

workspace/prd_resources.py

# -*- DjangoApp running on ECS
prd_django = DjangoApp(
    ...
    env={
        ...
        # Migrate database on startup using python manage.py migrate in entrypoint.sh
        "MIGRATE_DB": True,
    },
    ...
)

Delete AWS resources

Play around and then delete production resources using phi ws down prd:aws

phi ws down prd:aws

Next

Congratulations on running your own Django App. Next: