Skip to content
Advertisement

How to debug django staticfiles served with whitenoise, gunicorn, and heroku?

I’ve got a project that runs on Heroku from a Dockerfile and heroku.yml.

The site generally works, but I am having trouble with static files.

collectstatic is run when building the pack.

If I set DEBUG to True, it finds the files.

I’m trying to use whitenoise but not sure why it’s not working. It sounds so simple so I’m sure it’s something silly.

heroku.yml

setup:
    addons:
        - plan: heroku-postgresql
build:
    docker:
        web: Dockerfile
release:
    image: web
    command:
        - python manage.py collectstatic --noinput
run:
    web: gunicorn records_project.wsgi

settings.py

MIDDLEWARE = [
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
]
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    ... more stuff here...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# for referencing files with a URL
STATIC_URL = '/static/'
# where to find static files when local
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
# location of satatic files for production
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# how Django should look for static file directories; below is default
STATICFILES_FINDERS = [
    # defaults
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
# This gives me a 500 error
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

urls.py

urlpatterns here...
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Advertisement

Answer

For what it’s worth, I never found a way to get WhiteNoise to serve those static files. I swear it’s worked in the past with a similar set up, so that will remain a mystery.

I received a tip from Matt from justdjango.com that Heroku doesn’t want to serve static files from that same server. Once I moved my static files over to an AWS S3 bucket, all was well.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement