Question

Collecting static files on app platform for Django app using database env var

I’m trying to deploy a Django application using a Digital Ocean Postgres database and Redis instance on App Platform.

Here is my App Spec:

alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
databases:
- cluster_name: postgres
  engine: PG
  name: postgres
  production: true
  version: "13"
- cluster_name: redis
  engine: REDIS
  name: redis
  production: true
  version: "6"
envs:
- key: DJANGO_ALLOWED_HOSTS
  scope: RUN_TIME
  value: ${APP_DOMAIN}
- key: DATABASE_URL
  scope: RUN_TIME
  value: ${postgres.DATABASE_URL}
- key: REDIS_URL
  scope: RUN_TIME
  value: ${redis.REDIS_URL}
name: myapp
region: fra
services:
- environment_slug: python
  envs:
  - key: DISABLE_COLLECTSTATIC
    scope: RUN_AND_BUILD_TIME
    value: "1"
  github:
    branch: main
    deploy_on_push: true
    repo: mygithubuser/mygithubrepo
  http_port: 8080
  instance_count: 1
  instance_size_slug: basic-xxs
  name: myapp
  routes:
  - path: /
  run_command: gunicorn --worker-tmp-dir /dev/shm config.wsgi
  source_dir: /
jobs:
- name: migrate
  kind: PRE_DEPLOY
  github:
    repo: mygithubuser/mygithubrepo
    branch: main
    deploy_on_push: true
  run_command: python manage.py migrate
  envs:
  - key: DISABLE_COLLECTSTATIC
    value: "1"
    scope: BUILD_TIME
static_sites:
- name: static
  github:
    repo: mygithubuser/mygithubrepo
    branch: main
    deploy_on_push: true
  output_dir: staticfiles
  routes:
  - path: /static

I have tested this setup without the static_sites component and it builds and deploys OK, but I need my static files collected by Django with python manage.py collectstatic.

Thefore I’ve added the static_sites component but I now get the error:

       django.core.exceptions.ImproperlyConfigured: Set the {postgres.DATABASE_URL} environment variable

I believe this is because the environment variables DATABASE_URL=${postgres.DATABASE_URL} is not available at build time, only at run time?

If so, why is there an example of static files being collected in the example Django app here using this method?

https://github.com/digitalocean/sample-django/blob/main/.do/app.yaml

How can you make collectstatic a part of the build/deploy process?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hello,

Indeed, it seems like that you’ve set the scope of the database URL env variable to RUN_TIME only:

- key: DATABASE_URL
  scope: RUN_TIME
  value: ${postgres.DATABASE_URL}

You need to change the scope so that it is available in both the runtime and the build time scopes:

https://docs.digitalocean.com/products/app-platform/how-to/use-environment-variables/

Best,

Bobby