# Deploy Django to PandaStack: Step-by-Step Guide
Django is one of the most battle-tested Python web frameworks. Deploying it to production used to mean configuring Nginx, Gunicorn, Postgres, and SSL certificates yourself. PandaStack handles all of that — you focus on your application.
This guide walks through deploying a Django app with a managed PostgreSQL database on PandaStack.
Prerequisites
- A Django project pushed to a GitHub repository
- A free PandaStack account at [dashboard.pandastack.io](https://dashboard.pandastack.io)
- Python 3.10+ and Docker installed locally (for testing the Dockerfile)
Step 1: Prepare Your Django App for Production
Make sure your settings.py reads configuration from environment variables:
import os
from pathlib import Path
SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-only-insecure-key')
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT', '5432'),
}
}Or use dj-database-url for simplicity:
import dj_database_url
DATABASES = {'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))}Step 2: Create requirements.txt
django>=5.0
gunicorn>=21.0
psycopg2-binary>=2.9
dj-database-url>=2.0
whitenoise>=6.6Step 3: Write the Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python manage.py collectstatic --noinput
EXPOSE 8000
CMD ["gunicorn", "myproject.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "2"]Replace myproject with your actual Django project name (the directory containing wsgi.py).
Step 4: Create a Managed PostgreSQL Database
- 1Log in to [dashboard.pandastack.io](https://dashboard.pandastack.io)
- 2Navigate to Databases → New Database
- 3Select PostgreSQL
- 4Copy the connection string (format:
postgresql://user:password@host:5432/dbname)
Step 5: Deploy Your Django App
- 1Go to New Deployment → Container App
- 2Connect your GitHub repository
- 3PandaStack detects the Dockerfile automatically
- 4Add environment variables:
| Variable | Value |
|---|---|
DATABASE_URL | Your PostgreSQL connection string |
SECRET_KEY | A strong random secret key |
DEBUG | False |
ALLOWED_HOSTS | yourdomain.pandastack.io |
- 1Click Deploy
Step 6: Run Database Migrations
After your first deploy, run migrations using a PandaStack cronjob (one-time) or via the CLI:
panda exec --command "python manage.py migrate"Alternatively, add migrations to your Dockerfile CMD or an entrypoint script:
#!/bin/bash
python manage.py migrate --noinput
exec gunicorn myproject.wsgi:application --bind 0.0.0.0:8000 --workers 2Step 7: Configure Static Files
Add WhiteNoise to serve static files from the container:
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
# ... other middleware
]
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'Step 8: Connect a Custom Domain
Go to your deployment → Domains → Add Domain. Enter your domain, add the CNAME record in your DNS provider, and SSL is provisioned automatically.
Automating Future Deploys
Every push to your configured GitHub branch (default: main) triggers an automatic rebuild and deployment. PandaStack sends a notification (email, Slack, or webhook) on deploy success or failure.
Full docs: [docs.pandastack.io](https://docs.pandastack.io).