> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reliantlabs.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Django with Reliant Forge

> Deploy a Django app with Postgres from its own Dockerfile: Forge adopts it in place, runs python manage.py migrate before every rollout, and ships it to Reliant hosting behind gunicorn.

You will deploy an existing Django project, with a Dockerfile, to [Reliant hosting](https://docs.reliantlabs.io/hosting/overview.md) with a managed Postgres. Forge reads the Dockerfile, `requirements.txt` and `manage.py` and writes its declarations beside them; your Django code changes only where production settings need it (below). The detector output here is from Forge `v1.10.1-0.20261123064811-54313eead62f`.

## Before you begin

* Docker, and the project in a git repository with a root `Dockerfile` that builds on its own.
* Forge: `reliant forge` inside Reliant, or `npx -y @reliantlabs/forge@latest` anywhere else.
* For a deploy that lasts, a Reliant account with [app hosting billing](https://docs.reliantlabs.io/hosting/overview.md#billing).

## What the Dockerfile must do

Serve with gunicorn, bound to `0.0.0.0`, never `manage.py runserver`. Collect static files at build time:

```dockerfile Dockerfile theme={null}
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN DJANGO_SECRET_KEY=build-only python manage.py collectstatic --noinput
RUN useradd -r -u 10001 app
USER 10001
EXPOSE 8000
CMD ["gunicorn", "mysite.wsgi", "--bind", "0.0.0.0:8000"]
```

The detector reads the port from `EXPOSE` (or the compose service's published port, a `--bind`/`--port` in the command, or `ENV PORT`) and sets `PORT`. Serve the collected files with WhiteNoise (`whitenoise.middleware.WhiteNoiseMiddleware`): there is no nginx in front of the app. `collectstatic` imports your settings, so give it a throwaway key at build time instead of baking the real one into a layer.

## Production settings

Hosting serves the app on a generated `*.reliantlabs.dev` hostname, TLS terminated at the ingress. Read everything from the environment:

```python mysite/settings.py (excerpt) theme={null}
import os, dj_database_url

SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]          # no default: a missing key must fail
DEBUG = os.environ.get("DJANGO_DEBUG") == "1"
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "").split(",")
CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in ALLOWED_HOSTS if h]
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
DATABASES = {"default": dj_database_url.config(conn_max_age=60)}
```

`SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "dev-insecure")` is the classic hole: prod runs on a key that is in your repository. `ALLOWED_HOSTS` must hold the URL hosting gives you, so you learn it after the first deploy (`env status`) and set it then; until it does, every request is a 400. Without `CSRF_TRUSTED_ORIGINS`, Django 4+ rejects every form POST over HTTPS, the admin login included. `SECURE_PROXY_SSL_HEADER` stops `SECURE_SSL_REDIRECT` from looping.

## Postgres and migrations

With `psycopg` (or `psycopg2`) in `requirements.txt`, the detector adds a managed Postgres on staging and prod and passes its URL as `DATABASE_URL`; with `manage.py` present it adds a `migrate` job, `python manage.py migrate`, run from the app's image before every rollout. Don't also migrate in the container's start command.

**1. Preview and adopt**

```bash terminal theme={null}
reliant forge project new --in-place --kind app --dry-run
```

```text Output (trimmed) theme={null}
Detected:
  - workload api (service): builds Dockerfile, port 8000
  - Postgres: a managed Postgres on staging and prod; dev runs a forge host-native Postgres
  - migrations: python manage.py migrate (Django's manage.py) → job migrate from api's image, before every rollout
Assumed (each is a `# check:` line in deploy/kcl; review them):
  - dev runs api's Dockerfile CMD on the host (gunicorn mysite.wsgi --bind 0.0.0.0:$PORT): change it in deploy/kcl/dev/main.k if the app has a dev server
  - api uses Postgres (the Python dependencies name a Postgres driver) and reads its URL from DATABASE_URL
```

With no compose file, dev runs the command on your machine beside a local Postgres; change it to `python manage.py runserver 0.0.0.0:$PORT` in `deploy/kcl/dev/main.k` if you prefer. Then write the files and commit them:

```bash terminal theme={null}
reliant forge project new --in-place --kind app --name mysite
git add -A && git commit -m "Adopt with forge"
```

**2. Declare the settings and the secret**

With no compose file the detector can't see your env vars, so add them. In `deploy/kcl/prod/main.k`, on both the `api` and the `migrate` workload (`manage.py migrate` imports your settings too):

```python deploy/kcl/prod/main.k (excerpt) theme={null}
_hosted(wl.api) | {
    env.DATABASE_URL = _database_url
    env.DJANGO_SECRET_KEY = forge.ManagedSecret {name = "DJANGO_SECRET_KEY"}
    env.DJANGO_ALLOWED_HOSTS = "<your-host>.reliantlabs.dev"
}
_hosted(wl.migrate) | {
    env.DATABASE_URL = _database_url
    env.DJANGO_SECRET_KEY = forge.ManagedSecret {name = "DJANGO_SECRET_KEY"}
}
```

Replace the existing `api` and `migrate` bindings with these entries; don't add duplicate bindings. Both workloads reference the same managed secret, so set its value once:

```bash terminal theme={null}
python3 -c 'import secrets; print(secrets.token_urlsafe(50))' | reliant forge env secret set --env prod DJANGO_SECRET_KEY
```

Dev reads the same names from the gitignored `secrets/dev.yaml` (`DJANGO_SECRET_KEY: dev-only`) once you add the `ManagedSecret` to `deploy/kcl/dev/main.k`.

**3. Build, render, lint, deploy**

```bash terminal theme={null}
docker build .
reliant forge env render prod
reliant forge lint
reliant forge env deploy prod --yes
```

The deploy builds the image, runs `migrate`, then rolls out `api`. Without app hosting billing it is queued and exits **7** with the billing link. The default size is 250m CPU and 1 GiB per workload; two gunicorn sync workers fit comfortably.

With no account, `npx -y @reliantlabs/forge@latest env deploy prod --claimable` publishes a 72-hour preview (adopt with `--kind app --claimable` first, and run `--plan-only` to check it fits). It is one service at 125m CPU and **128 MiB**, so run a single gunicorn worker (`--workers 1`); it gets no secret values and no outbound internet until claimed. A `SECRET_KEY` read with `os.environ[...]` therefore won't start unclaimed: claim first, or sign in. See [Deploy without an account](https://docs.reliantlabs.io/forge/deploy-without-an-account.md).

**4. Verify**

```bash terminal theme={null}
reliant forge env status prod
curl -sI https://<your-url>/admin/login/
reliant forge obs check prod --wait --json
```

A `200` from the admin login proves `ALLOWED_HOSTS`, static files and the database connection at once. `obs check` exits **0** proven good, **1** regressed, **2** not proven, **8** the wait ran out. Without an OpenTelemetry SDK (`opentelemetry-instrumentation-django`) it is 2, `no_request_telemetry`: not proven good. See [Send OpenTelemetry from any app](https://docs.reliantlabs.io/guides/observability/send-opentelemetry-from-any-app.md).

## Django gotchas

* **400 Bad Request on every page:** the host is not in `ALLOWED_HOSTS`. Add it, redeploy.
* **403 CSRF verification failed on POST:** `CSRF_TRUSTED_ORIGINS` lacks `https://<host>`.
* **Celery:** a worker is a second workload (`kind = "worker"`, same image, `command = ["celery", "-A", "mysite", "worker"]`). It needs billing; an anonymous deploy allows one service only.
* **Media uploads:** the container disk is scratch and is lost on every rollout. Use object storage.

See also: [Bring an existing app](https://docs.reliantlabs.io/guides/forge/bring-existing-app-any-language.md), [Managed Postgres](https://docs.reliantlabs.io/hosting/managed-postgres.md), [Secrets](https://docs.reliantlabs.io/hosting/secrets.md), [Hosting limits](https://docs.reliantlabs.io/hosting/limits.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.