Skip to content

Django app

This example deploys a Django demo app with dpl.

It covers five common tasks:

  • run MariaDB
  • create a database and user
  • deploy a Django app with migrations
  • serve the app and static files through nginx
  • run a timer once per minute

Database server:

type: db-server
engine: mariadb
version: 12
secret: db-root

Database:

type: db
server: mariadb
user: demo
secret: db-demo

App:

type: app
image: python:3.13-slim
builds:
- files: ["requirements.txt"]
script: |
pip install --no-cache-dir -r requirements.txt
- files: ["*"]
env:
DB_NAME: ${db-demo:name}
DB_USER: ${db-demo:user}
DB_PASSWORD: ${db-demo:password}
DB_HOST: ${db-demo:host}
DB_PORT: ${db-demo:port}
script: |
python manage.py collectstatic --noinput
mkdir -p /app/staticfiles
runtime:
port: 8000
env:
ALLOWED_HOSTS: "*"
DJANGO_SECRET_KEY: django-demo-insecure-key
DB_NAME: ${db-demo:name}
DB_USER: ${db-demo:user}
DB_PASSWORD: ${db-demo:password}
DB_HOST: ${db-demo:host}
DB_PORT: ${db-demo:port}
init: |
python manage.py migrate --noinput
cmd: "gunicorn app.wsgi:application --bind 0.0.0.0:8000 --workers 2 --timeout 120"
exports:
- source: /app/staticfiles
path: /static
timers:
- name: add-time
schedule: "* * * * *"
script: |
python manage.py add_time --seconds 60

HTTP server:

type: http-server
http_port: 8000
https_port: false

Domain:

type: domain
server: nginx
hosts: ["127.0.0.1"]
routes:
- kind: reverse_proxy
location: /
target: ${django-demo:url}
- kind: serve_files
location: /static
root: ${django-demo:export}

Use DPL_BASE to point dpl at the demo directory:

Terminal window
export DPL_BASE="$PWD/django-demo"

Create the secrets if you are starting from a clean copy:

Terminal window
dpl secret create db-root
dpl secret create db-demo

Check the units:

Terminal window
dpl check db-demo
dpl check django-demo

Start dpl serve in another terminal:

Terminal window
DPL_BASE="$PWD/django-demo" dpl serve

Pack the app into an archive, then deploy the database and the app:

Terminal window
tar -czf /tmp/django-demo.tar.gz -C django-demo/app .
dpl deploy db-demo
dpl deploy django-demo /tmp/django-demo.tar.gz

Inspect the app:

Terminal window
dpl inspect django-demo

Open the site at:

http://127.0.0.1:8000

If a build, the app, or a timer fails, check these logs:

django-demo/state/django-demo/log/build.log
django-demo/state/django-demo/log/runtime.log
django-demo/state/django-demo/log/timers.log