# Deploy Django on forkrail

> Automatic `ASGI_APPLICATION` / `WSGI_APPLICATION` detection from `manage.py`, build-time `collectstatic` served by the router, KMS secrets, and same-account RDS access.

- Framework: Django
- Category: APIs & backends
- Status: Production ready
- Adapter / Preset: None (auto-reads manage.py settings module)
- Runtime: Python 3.13 arm64 (ASGI or 10-thread WSGI)
- Detection: manage.py + Django dependency
- Memory: 640–1,024 MB
- URL: https://www.forkrail.com/frameworks/django

## Capabilities

| Capability | Support | Details |
| --- | --- | --- |
| ASGI & WSGI | Supported | Native ASGI, or WSGI via a2wsgi (10 threads per worker) |
| collectstatic | Supported | Runs at build when STATIC_ROOT is set; router serves STATIC_URL |
| KMS secrets | Supported | SECRET_KEY and DATABASE_URL encrypted under your AWS KMS key |
| Cron management | Supported | Trigger maintenance endpoints from vercel.json schedules |

## 1. Prepare Django settings

forkrail inspects `manage.py` to find your settings module and boots `ASGI_APPLICATION` (or falls back to `WSGI_APPLICATION` running 10 concurrent threads under `a2wsgi`). Make sure `ALLOWED_HOSTS` allows your `.forkrail.com` preview hostnames and custom domains:

**config/settings.py**

```python
import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
DEBUG = False

ALLOWED_HOSTS = [
    '.forkrail.com',
    'app.acme.dev',
]

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
```

Whenever `STATIC_ROOT` is configured, forkrail runs `collectstatic` during the build and uploads the static files to S3 so the regional router serves `STATIC_URL` without invoking Python (including `staticfiles.json` for manifest storage).

## 2. Store secrets & deploy

**Add Django environment variables and deploy**

```bash
printf 'django-insecure-replace-me' | forkrail env add portal DJANGO_SECRET_KEY --target production
printf 'postgres://user:pass@db.internal:5432/portal' | forkrail env add portal DATABASE_URL --target production

forkrail deploy --prod
```

> **Co-locate with your RDS database:** Set `"regions": ["iad1"]` (or whichever AWS region holds your RDS instance) in `vercel.json` so database queries stay inside one region with millisecond latency.

## Related documentation

- [Function regions](https://www.forkrail.com/docs/function-regions.md)
- [Environment variables](https://www.forkrail.com/docs/environment-variables.md)
- [Custom domains](https://www.forkrail.com/docs/domains.md)
