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
- Create App → Select Repo → Select Branch
- Check “Autodeploy”
Resources
Web Service
- Edit Plan → Select “Basic” → Select “$5.00/mo – Basic” → Back
- Edit Web Service → Edit Run Command → Save
gunicorn --worker-tmp-dir /dev/shm <YOUR_PROJECT_NAME>.wsgi
Database
- Add Resource → Database → Add
- Create and Attach
Static Site
- Add Resource → Detect from Source Code → Add
- Select Provider → Select Repo/Branch → Check “Autodeploy” → Next
- Edit new Web Service
- Rename to
<PROJECT>-static
→ Save - Update “Resource Type” to “Static Site” → Save
- Update “Output Directory” to
staticfiles
→ Save - Update “HTTP Request Routes” to
/static
→ Save
Environment Variables
- Edit Web Service resource
- Add the following environment variables:
Key | Value | Encrypt |
---|---|---|
DATABASE_URL | ${db.DATABASE_URL} | N |
DJANGO_ALLOWED_HOSTS | ${APP_DOMAIN} | N |
DEBUG | False | N |
ADMIN_URL | your-secret-admin/ | N |
DJANGO_SECRET_KEY | your-secret-key | Y |
- Save
Info
- Rename project, if desired → Save
Review
- Click “Create Resources”
Post Deploy
- Navigate to “Console” tab
- 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.
I wish it could be simpler, but this is the best I got so far. ↩︎
Basic plans start around $12/mo. ↩︎
I made a few improvements like adding
ADMIN_URL
andSECURE_HSTS_SECONDS
to achieve a 100% score on djcheckup.com. ↩︎Assumption: You have a simple Django project. ↩︎
Assumption: You have committed the project to a git repo and have a DigitalOcean account. ↩︎