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-appCreate a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activateInstall Django and production dependencies:
pip install django gunicorn whitenoiseCreate a new Django project:
django-admin startproject myproject .Create requirements.txt:
pip freeze > requirements.txtConfigure for Production
Open myproject/settings.py and make the following changes:
Import os module
At the top of the file:
import osConfigure 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'Configure Secret Key (Optional but Recommended)
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.sqlite3Test Locally
Run database migrations:
python manage.py migrateRun the development server:
python manage.py runserverOpen 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:8000Push 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 mainReplace YOUR_USERNAME with your GitHub username.
Deploy on Out Plane
Step 1: Install GitHub App
- Go to console.outplane.com
- Navigate to Applications from the sidebar
- Click Deploy Application
- If prompted, click Install GitHub App
- Select which repositories Out Plane can access
- Click Install and you'll be redirected back
Step 2: Select Repository
- Find your repository in the list
- Click to select it
- Choose the branch to deploy (default:
main) - Set Root Directory to
/
Step 3: Configure Build
| Setting | Value |
|---|---|
| Build Method | Select Dockerfile |
| Application Name | Enter a unique name (e.g., my-django-app) |
| Port | 8000 |
Step 4: Select Resources
| Instance | vCPU | RAM | Recommended For |
|---|---|---|---|
| op-20 | 0.5 | 512 MB | Development |
| op-46 | 1 | 1 GB | Small apps |
| op-82 | 2 | 2 GB | Production (recommended) |
| op-94 | 4 | 4 GB | High traffic |
Step 5: Configure Scaling
- Min Scale: Set to
1for always-on, or0for scale-to-zero - Max Scale: Set based on expected traffic
Step 6: Environment Variables
Click Add Variable to add required environment variables:
| Key | Value |
|---|---|
DJANGO_ALLOWED_HOSTS | my-django-app.outplane.app |
DJANGO_SECRET_KEY | A secure random string |
DEBUG | False |
Always set DJANGO_ALLOWED_HOSTS to your Out Plane domain. Without this, Django will reject all requests.
Step 7: Deploy
- Review your configuration
- Click Create Application
- Wait for the build to complete (typically 2-4 minutes)
Step 8: Access Your App
Once the deployment status shows Ready:
- Go to the application Overview tab
- Find your domain:
my-django-app.outplane.app - Click to open your live application
- Access admin panel at
my-django-app.outplane.app/admin/
Adding a Database
Step 1: Create a Database
- Go to Databases in the sidebar
- Click Create Database
- Choose a name, PostgreSQL version, and region
- Click Create Database
Step 2: Get Connection String
- Click on your database
- In the Overview tab, find the connection string
- Copy the connection URL
Step 3: Install PostgreSQL Driver
pip install psycopg2-binary dj-database-url
pip freeze > requirements.txtStep 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
- Go to your application in Out Plane
- Click Settings → Environment
- Add
DATABASE_URLwith your connection string - 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:applicationCreating 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 mainNext Steps
Buildpack alternative: Instead of Dockerfile, select Buildpack and add a Procfile with: web: gunicorn myproject.wsgi --bind 0.0.0.0:$PORT