Out Plane LogoOut Plane

Django

Step-by-step guide to deploy a Django application on Out Plane

This guide walks you through deploying a Django application on Out Plane using Git-driven deployment with a Dockerfile.

Prerequisites

  • An Out Plane account
  • A GitHub account
  • Python 3.10+ installed on your machine
  • Git installed on your machine

Create the Django App

Create a new directory for your project:

mkdir my-django-app
cd my-django-app

Create a virtual environment and activate it:

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

Install Django and production dependencies:

pip install django gunicorn whitenoise

Create a new Django project:

django-admin startproject myproject .

Create requirements.txt:

pip freeze > requirements.txt

Configure for Production

Open myproject/settings.py and make the following changes:

Import os module

At the top of the file:

import os

Configure Allowed Hosts

Update the ALLOWED_HOSTS setting to read from environment variables:

ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "localhost,127.0.0.1").split(",")

Add WhiteNoise Middleware

Add WhiteNoise to the MIDDLEWARE list, right after SecurityMiddleware:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',  # Add this line
    'django.contrib.sessions.middleware.SessionMiddleware',
    # ... rest of middleware
]

Configure Static Files

Update the static files settings at the bottom of settings.py:

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

For production, read the secret key from environment:

SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "your-default-secret-key-for-dev")

Create a Simple View

Create a simple view to test the deployment. Open myproject/urls.py:

from django.contrib import admin
from django.urls import path
from django.http import JsonResponse

def home(request):
    return JsonResponse({"message": "Hello from Out Plane"})

def health(request):
    return JsonResponse({"status": "healthy"})

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home),
    path('health/', health),
]

Create the Dockerfile

Create a file called Dockerfile in your project root:

FROM python:3.12-slim AS builder

WORKDIR /app

RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

FROM python:3.12-slim

WORKDIR /app

COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY . .

RUN python manage.py collectstatic --noinput

EXPOSE 8000

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "myproject.wsgi:application"]

Create a .dockerignore file:

__pycache__
*.pyc
.git
.env
venv
.venv
*.md
.DS_Store
staticfiles
db.sqlite3

Test Locally

Run database migrations:

python manage.py migrate

Run the development server:

python manage.py runserver

Open http://localhost:8000 in your browser. You should see the JSON response.

Test with Gunicorn (production server):

gunicorn myproject.wsgi:application --bind 0.0.0.0:8000

Push to GitHub

Initialize a Git repository and push to GitHub:

git init
git add .
git commit -m "Initial commit"
git remote add origin [email protected]:YOUR_USERNAME/my-django-app.git
git branch -M main
git push -u origin main

Replace YOUR_USERNAME with your GitHub username.

Deploy on Out Plane

Step 1: Install GitHub App

  1. Go to console.outplane.com
  2. Navigate to Applications from the sidebar
  3. Click Deploy Application
  4. If prompted, click Install GitHub App
  5. Select which repositories Out Plane can access
  6. Click Install and you'll be redirected back

Step 2: Select Repository

  1. Find your repository in the list
  2. Click to select it
  3. Choose the branch to deploy (default: main)
  4. Set Root Directory to /

Step 3: Configure Build

SettingValue
Build MethodSelect Dockerfile
Application NameEnter a unique name (e.g., my-django-app)
Port8000

Step 4: Select Resources

InstancevCPURAMRecommended For
op-200.5512 MBDevelopment
op-4611 GBSmall apps
op-8222 GBProduction (recommended)
op-9444 GBHigh traffic

Step 5: Configure Scaling

  • Min Scale: Set to 1 for always-on, or 0 for scale-to-zero
  • Max Scale: Set based on expected traffic

Step 6: Environment Variables

Click Add Variable to add required environment variables:

KeyValue
DJANGO_ALLOWED_HOSTSmy-django-app.outplane.app
DJANGO_SECRET_KEYA secure random string
DEBUGFalse

Always set DJANGO_ALLOWED_HOSTS to your Out Plane domain. Without this, Django will reject all requests.

Step 7: Deploy

  1. Review your configuration
  2. Click Create Application
  3. Wait for the build to complete (typically 2-4 minutes)

Step 8: Access Your App

Once the deployment status shows Ready:

  1. Go to the application Overview tab
  2. Find your domain: my-django-app.outplane.app
  3. Click to open your live application
  4. Access admin panel at my-django-app.outplane.app/admin/

Adding a Database

Step 1: Create a Database

  1. Go to Databases in the sidebar
  2. Click Create Database
  3. Choose a name, PostgreSQL version, and region
  4. Click Create Database

Step 2: Get Connection String

  1. Click on your database
  2. In the Overview tab, find the connection string
  3. Copy the connection URL

Step 3: Install PostgreSQL Driver

pip install psycopg2-binary dj-database-url
pip freeze > requirements.txt

Step 4: Configure Database in settings.py

import dj_database_url

DATABASE_URL = os.getenv("DATABASE_URL")

if DATABASE_URL:
    DATABASES = {
        'default': dj_database_url.parse(DATABASE_URL)
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }

Step 5: Add Environment Variable

  1. Go to your application in Out Plane
  2. Click SettingsEnvironment
  3. Add DATABASE_URL with your connection string
  4. Click Save and redeploy

Step 6: Run Migrations

You can run migrations by connecting to your app or including them in your startup. Update your Dockerfile CMD:

CMD python manage.py migrate && gunicorn --bind 0.0.0.0:8000 --workers 2 myproject.wsgi:application

Creating a Superuser

To create an admin superuser, you can use the createsuperuser command with environment variables. Add these to your settings.py:

DJANGO_SUPERUSER_USERNAME = os.getenv("DJANGO_SUPERUSER_USERNAME")
DJANGO_SUPERUSER_EMAIL = os.getenv("DJANGO_SUPERUSER_EMAIL")
DJANGO_SUPERUSER_PASSWORD = os.getenv("DJANGO_SUPERUSER_PASSWORD")

Then run once during deployment by temporarily adding to your startup command.

Automatic Deployments

Push to your branch to trigger automatic deployments:

git add .
git commit -m "Add new feature"
git push origin main

Next Steps

Buildpack alternative: Instead of Dockerfile, select Buildpack and add a Procfile with: web: gunicorn myproject.wsgi --bind 0.0.0.0:$PORT