Question

App Platform for Django App

Posted October 27, 2020 332 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

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

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

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

Submit an Answer