Deploy a Django App

This tutorial walks you through deploying a Django application on Temps with PostgreSQL, static file serving, and Gunicorn. By the end, you will have a production Django app with automatic HTTPS and deployments on every push.


What you will build

  • A Django app deployed from a Git repository
  • PostgreSQL database provisioned and connected
  • Static files collected and served via WhiteNoise
  • Gunicorn as the production WSGI server
  • Automatic HTTPS and deployment on every push

Time required: 15 minutes.

Prerequisites:

  • A running Temps instance (install guide)
  • A Django app in a Git repository
  • (Optional) A custom domain

Step 1: Prepare your app

Add production dependencies

Make sure your requirements.txt includes a production WSGI server and static file handler:

requirements.txt

django>=5.0
gunicorn>=22.0
whitenoise>=6.0
psycopg2-binary>=2.9

Configure settings for production

Update your settings.py to read from environment variables:

settings.py

import os

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'change-me')
DEBUG = os.environ.get('DJANGO_DEBUG', 'False') == 'True'
ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '*').split(',')

# Database — use DATABASE_URL from Temps managed services
import dj_database_url
DATABASES = {
    'default': dj_database_url.config(
        default=os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite3')
    )
}

# Static files with WhiteNoise
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MIDDLEWARE.insert(1, 'whitenoise.middleware.WhiteNoiseMiddleware')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Add a health check endpoint

urls.py

from django.http import JsonResponse

def health(request):
    return JsonResponse({'status': 'ok'})

urlpatterns = [
    # ... your existing URLs
    path('health/', health),
]

Create a Procfile or Dockerfile

Temps auto-detects Python projects. For Django, provide a start command:

Procfile

web: gunicorn myproject.wsgi:application --bind 0.0.0.0:$PORT --workers 3

Or use a Dockerfile for full control:

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", "3"]

Step 2: Create a project

  1. Open the Temps dashboard
  2. Click New Project
  3. Enter a name (e.g., my-django-app)
  4. Click Create

Or use the CLI:

bunx @temps-sdk/cli projects create -n "my-django-app" -d "Django application"

Step 3: Provision a database

Django needs PostgreSQL in production. Create a managed database in Temps:

  1. Go to your project → Services
  2. Click Add ServicePostgreSQL
  3. Temps creates the database and sets DATABASE_URL automatically

Or via CLI:

bunx @temps-sdk/cli services create -p my-django-app -t postgres -n "django-db"

The DATABASE_URL environment variable is automatically injected into your app's environment.


Step 4: Connect your repository

  1. Go to your project → SettingsGit
  2. Connect your GitHub, GitLab, Bitbucket, or Gitea account
  3. Select your repository and branch

Or via CLI:

bunx @temps-sdk/cli projects git -p my-django-app --owner yourorg --repo my-django-app --branch main --preset python -y

Step 5: Set environment variables

Go to your project → Environment Variables and add:

VariableValueNotes
DJANGO_SECRET_KEY(generate a random key)Required for production
DJANGO_DEBUGFalseNever run debug in production
DJANGO_ALLOWED_HOSTSyourdomain.comYour domain name

DATABASE_URL is set automatically by the managed PostgreSQL service.


Step 6: Deploy

Push to your connected branch, or deploy manually:

bunx @temps-sdk/cli deploy my-django-app -b main -e production -y

Temps will:

  1. Detect your Python/Django project
  2. Build the Docker image
  3. Run collectstatic during build
  4. Start Gunicorn on the configured port
  5. Run health checks before routing traffic
  6. Issue an SSL certificate for your domain

Step 7: Run migrations

After your first deploy, run Django migrations:

bunx @temps-sdk/cli exec my-django-app -- python manage.py migrate

For subsequent deployments, add migrations to your build or release command in the Dockerfile:

RUN python manage.py collectstatic --noinput
CMD ["sh", "-c", "python manage.py migrate && gunicorn myproject.wsgi:application --bind 0.0.0.0:8000 --workers 3"]

What to do next

Was this page helpful?