Question

App Platform for Django App

Posted October 27, 2020 1.3k views
Django

How do you deploy a Django application on the App platform? Specifically, how do you add/configure Nginx to serve static files?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
3 answers

This may help as well; I put together an app spec YAML that models a Django app with a service, static site for staticfiles, migration job, and DB:

name: sample-django
services:
- name: server
  github:
    repo: digitalocean/sample-django
    branch: static
    deploy_on_push: true
  run_command: python manage.py runserver 0.0.0.0:${PORT:-8080}
  envs:
  - key: DISABLE_COLLECTSTATIC
    value: "1"
    scope: BUILD_TIME
  - key: DATABASE_URL
    value: "${db.DATABASE_URL}"
    scope: RUN_TIME
  - key: DJANGO_ALLOWED_HOSTS
    value: "${APP_DOMAIN}"
    scope: RUN_TIME
jobs:
- name: migrate
  kind: PRE_DEPLOY
  github:
    repo: digitalocean/sample-django
    branch: static
    deploy_on_push: true
  run_command: python manage.py migrate
  envs:
  - key: DISABLE_COLLECTSTATIC
    value: "1"
    scope: BUILD_TIME
  - key: DATABASE_URL
    value: "${db.DATABASE_URL}"
    scope: RUN_TIME
static_sites:
- name: static
  github:
    repo: digitalocean/sample-django
    branch: static
    deploy_on_push: true
  # This happens as part of the Python/DJango buildpack:
  # build_command: python manage.py collectstatic --noinput
  output_dir: staticfiles
  routes:
  - path: /static
databases:
# Create a new dev DB:
- name: db
  engine: PG
  version: "12"
# Or bring an existing DB:
# - name: db
#   production: true
#   cluster_name: mydb
#   engine: PG
#   version: "12"

This can be submitted using doctl with:

doctl apps create --spec .do/app.yaml

Hello @idiallo

Yes, it’s possible to host a Django app in App Platform. We have a sample Django app in our GitHub repo which you can consider as an example and make necessary changes in your app:

https://github.com/digitalocean/sample-django

Please note that at least one of the requirements.txt, Pipfile, and setup.py must be located in the root directory of the repo.

Here’s the documentation for more information:
https://www.digitalocean.com/docs/app-platform/languages-frameworks/python/

Static files are served using global CDN and there is no specific configuration done in the webserver for static files.

Cheers,

Dikshith

  • Thank you so much for your timely reply. Leveraging CDN to serve static file is justifiable for medium to large side application. For relatively small applications, it would be simpler and more cost effective to serve static files directly from the application. The workaround most developers have been using prior to the App platform is to leverage Nginx. My question was:

    1)Is it possible to use Nginx to serve static files in the App platform?
    2) How that configuration is done?
    3) Are there sample apps using nginx in the app platform environment?

    Thanks again,
    ID

Your setup does not include Nginx at all. Can you run a prod application with only Gunicorn?

  • So, yes and no. You can totally run a production application with only gunicorn, but you’ll want something to serve the static media. The app spec above pushes static media off to DigitalOcean’s CDN and tells Django to look there for static media.

    Effectively, instead of serving images/fonts/css files from <yourserver>/static/ like you’re probably used to, Django will just load them from <your_digitalocean_cdn>/static/, which is fine (usually better, actually, because you can configure the CDN to various points of presence so users in New York get their media from servers in New York, while users in San Francisco will get their media from servers in San Francisco)

    In that scenario, you totally don’t need Nginx unless there’s something you want done (like a reverse proxy to a remote API endpoint) that Nginx does other than serving media, and that also is not provided by the DigitalOcean ecosystem. If you’re just looking for Nginx to host static media, you 100% don’t need it here.

Submit an Answer