Managing Django Static Files on Heroku

Deploying and running Django apps on Heroku is really a no-brainer, except for one thing — serving static files via collectstatic.

I run collectstatic as usual, using heroku run command just like how I did it for syncdb. It worked but I’m getting 404 error when serving static files.

It turns out that running collectstatic via heroku run spins up a new dyno and collectstatic is running in an isolated environment. So, all the collected static files are deployed to that location and only for this new dyno and the dyno attached to our Django app can’t access that. — Heroku support staff


The dead simple solution would be to run collectstatic as part of the Procfile before starting the app. We need to “chain” together our Procfile commands for the web process, like so:

OK there you go, no more 404 error when serving static files from your Django app on Heroku. Plus, every time you deploy your app, newly added static files will be collected automatically.


There are a lot of questions about the configurations. Cross check with my here

Important thing here is your STATICFILES_DIRS. Make sure to include your project_name/app_name/static here. In my case, I have project_name/staticfiles for the STATIC_ROOT. Change STATIC_URL = ‘/static/’ if you want to serve from Heroku, same goes to ADMIN_MEDIA_PREFIX = ‘/static/admin/’

Finally add this to your in order to serve from Heroku.

urlpatterns += patterns('',
 (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

Files could be access as such:

/app/project_name/staticfiles/style.css >
/app/lib/python2.7/site-packages/django/contrib/admin/media/css/login.css >

Please note that this is not the best way to serve static files but it works. One last thing, you still need to go through all these things if you are using django storages and django compressor to serve static files via Amazon S3 because django compressor needs a temp file system cache on Heroku.