Guides

Python (Django)

Deploy Django with gunicorn — start command, ALLOWED_HOSTS, migrations, DATABASE_URL and common failures.

Create the app

  • Apps → New app, pick the repository. A manage.py at the root makes it “Python service (Django)”. Choose Python Service.
  • With manage.py detected, the start command is filled in as python manage.py runserver 0.0.0.0:$PORT — Django’s development server. For production, change it to gunicorn (below) under Settings → Runtime configuration before the first deploy.

What the repo needs

requirements.txt
Django
gunicorn
dj-database-url
psycopg[binary]   # PostgreSQL driver
settings.py (the parts that matter)
import os
import dj_database_url

SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
DEBUG = os.environ.get("DJANGO_DEBUG") == "1"
ALLOWED_HOSTS = ["*"]  # see the note below
DATABASES = {"default": dj_database_url.config(conn_max_age=600)}  # reads DATABASE_URL
ALLOWED_HOSTS: the health check calls your container by its internal name, not your domain. A list that only names your domains answers it with 400 Bad Request, and the release never goes live. Only hosts routed to your environment reach the app, so ["*"] is the simple, safe choice here.

Settings

Install command:    python -m venv .venv && .venv/bin/pip install -r requirements.txt
Build command:      (empty)
Start command:      python manage.py migrate && gunicorn mysite.wsgi --bind 0.0.0.0:$PORT
Health check path:  /
  • Replace mysite with the package that holds wsgi.py. Detection suggests python manage.py runserver 0.0.0.0:$PORT, which works but is Django’s development server; gunicorn is the production choice.
  • Running migrate in the Start command applies migrations on every release before the server starts. .venv/bin is on PATH at runtime, so python and gunicorn come from your venv.
  • Static files: with DEBUG off, Django doesn’t serve them itself. A common setup is WhiteNoise plus a Build command of .venv/bin/python manage.py collectstatic --noinput (the build step doesn’t put .venv/bin on PATH, so call it by path).

Common failures

  • “Python start command couldn’t run” (gunicorn: not found) — gunicorn isn’t in requirements.txt, or the install command doesn’t create .venv.
  • “App didn’t respond on its assigned port” with HTTP 400 in the health check lines — ALLOWED_HOSTS is rejecting the health check (see above). With no response at all — the server isn’t bound to 0.0.0.0:$PORT, or it crashed on boot (a missing DJANGO_SECRET_KEY raises KeyError).
  • “Database has no tables (migrations not run)” — add python manage.py migrate && in front of the Start command.
  • “Can’t connect to the database” — DATABASE_URL isn’t set. Attach a managed database with “Use in app”, then redeploy.