Jonathan Peacher

Deploy Django with DigitalOcean App Platform

I’ve tried a variety of different ways to deploy Django projects over the last five years, and they all seem a bit overcomplicated. I would very much like to focus less on the infrastructure part of web development, and more on making cool stuff, particularly with Django. To that end, I’ve been looking for a simple1 and cheap2 way to throw a Django projects onto the internet, and I think DigitalOcean’s App Platform is the best I’ve found so far.

The rest of this post is based3 on DigitalOcean’s tutorial, but stripped down for anyone who just wants a TLDR on how to chuck a Django project4 onto the interwebs!

Step 1: Install Python Packages

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

Step 2: Update settings.py

import os
import sys
import dj_database_url

SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", 'django-insecure-xyz')

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

ADMIN_URL = os.getenv("ADMIN_URL", "admin/")

SECURE_HSTS_SECONDS = 31536000

if DEBUG is True:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
        }
    }
elif len(sys.argv) > 0 and sys.argv[1] != 'collectstatic':
    if os.getenv("DATABASE_URL", None) is None:
        raise Exception("DATABASE_URL environment variable not defined")
    DATABASES = {
        "default": dj_database_url.parse(os.environ.get("DATABASE_URL")),
    }

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

Step 3: Deploy on App Platform

Navigate to the App Platform5.

Create App

  1. Create App → Select Repo → Select Branch
  2. Check “Autodeploy”

Resources

Web Service

  1. Edit Plan → Select “Basic” → Select “$5.00/mo – Basic” → Back
  2. Edit Web Service → Edit Run Command → Save
gunicorn --worker-tmp-dir /dev/shm <YOUR_PROJECT_NAME>.wsgi

Database

  1. Add Resource → Database → Add
  2. Create and Attach

Static Site

  1. Add Resource → Detect from Source Code → Add
  2. Select Provider → Select Repo/Branch → Check “Autodeploy” → Next
  3. Edit new Web Service
  4. Rename to <PROJECT>-static → Save
  5. Update “Resource Type” to “Static Site” → Save
  6. Update “Output Directory” to staticfiles → Save
  7. Update “HTTP Request Routes” to /static → Save

Environment Variables

  1. Edit Web Service resource
  2. Add the following environment variables:
KeyValueEncrypt
DATABASE_URL${db.DATABASE_URL}N
DJANGO_ALLOWED_HOSTS${APP_DOMAIN}N
DEBUGFalseN
ADMIN_URLyour-secret-admin/N
DJANGO_SECRET_KEYyour-secret-keyY
  1. Save

Info

  1. Rename project, if desired → Save

Review

  1. Click “Create Resources”

Post Deploy

  1. Navigate to “Console” tab
  2. Run the following commands:
python manage.py migrate
python manage.py createsuperuser

That it, your Django project should now be live! 🚀 Hopefully I laid that out as simply as possible, but feel free to checkout out DigitalOcean’s tutorial if you’d like more detail.


  1. I wish it could be simpler, but this is the best I got so far. ↩︎

  2. Basic plans start around $12/mo. ↩︎

  3. I made a few improvements like adding ADMIN_URL and SECURE_HSTS_SECONDS to achieve a 100% score on djcheckup.com↩︎

  4. Assumption: You have a simple Django project. ↩︎

  5. Assumption: You have committed the project to a git repo and have a DigitalOcean account. ↩︎

Reply via email