[Answer]-Serve static file with herokku, django and wsgi

1👍

Serving static files on Heroku with Django is a bit tricky. Assuming you’re using the ‘staticfiles’ app, you have to run ‘collectstatic’ to collect your static files after deploying. The problem with Heroku is that running ‘collectstatic’ in the shell will actually run in a new dyno, which disappears as soon as it’s finished.

One potential solution is outlined here:

Basically, the idea is to combine a few commands in your Procfile so that ‘collectstatic’ is run during the dyno spinup process:

web: python my_django_app/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_django_app/settings.py 

You also have to add the ‘static’ views to your urls.py (see https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user, but duplicate for STATIC_URL and STATIC_ROOT). It’s worth noting that the Django docs recommend against using this in production.

This solution isn’t ideal though, since you are still using your gunicorn process to serve static files. IMHO the best approach to dealing with static files on Heroku is to host them on something like S3.

Leave a comment