Search code examples
djangoherokudjango-staticfiles

Heroku doesn't collect static on Django but tell that it does


Command heroku run python manage.py collectstatic returns me something like

163 static files copied to '/app/live-static-files/static-root', 
509 post-processed.

Here are my settings:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # White Noise
    'django.contrib.sessions.middleware.SessionMiddleware',
    ...
]
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "live-static-files", "static-root")

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "live-static-files", "media-root")

DISABLE_COLLECTSTATIC = 0

Through heroku run bash i go /app/live-static-files/static-root and see it's empty (the folder exists as it's in repo with .gitkeep) and server gives 500 error on all requests.

Now with heroku run bash i try python manage.py collectstatic and it works! But after heroku restart it's empty again.

Why Heroku tell that it copied statics, even tell the right path of the statics folder but in fact doesn't do that? Why it actually doesn't do that?


Solution

  • The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy. This is similar to how many container based systems, such as Docker, operate.

    In addition, under normal operations dynos will restart every day in a process known as "Cycling".

    Take a read of these articles:

    https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted

    https://devcenter.heroku.com/articles/django-assets