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
JavaScript
x
13
13
1
setup:
2
addons:
3
- plan: heroku-postgresql
4
build:
5
docker:
6
web: Dockerfile
7
release:
8
image: web
9
command:
10
- python manage.py collectstatic --noinput
11
run:
12
web: gunicorn records_project.wsgi
13
settings.py
JavaScript
1
13
13
1
MIDDLEWARE = [
2
'django.middleware.cache.UpdateCacheMiddleware',
3
'django.middleware.security.SecurityMiddleware',
4
'whitenoise.middleware.WhiteNoiseMiddleware',
5
'django.contrib.sessions.middleware.SessionMiddleware',
6
'django.middleware.common.CommonMiddleware',
7
'django.middleware.csrf.CsrfViewMiddleware',
8
'django.contrib.auth.middleware.AuthenticationMiddleware',
9
'django.contrib.messages.middleware.MessageMiddleware',
10
'django.middleware.clickjacking.XFrameOptionsMiddleware',
11
'django.contrib.sites.middleware.CurrentSiteMiddleware',
12
]
13
JavaScript
1
11
11
1
INSTALLED_APPS = [
2
'django.contrib.admin',
3
'django.contrib.auth',
4
'django.contrib.contenttypes',
5
'django.contrib.sessions',
6
'django.contrib.messages',
7
'whitenoise.runserver_nostatic',
8
'django.contrib.staticfiles',
9
'django.contrib.sites',
10
more stuff here
11
JavaScript
1
20
20
1
# Static files (CSS, JavaScript, Images)
2
# https://docs.djangoproject.com/en/3.0/howto/static-files/
3
4
MEDIA_URL = '/media/'
5
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
6
# for referencing files with a URL
7
STATIC_URL = '/static/'
8
# where to find static files when local
9
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
10
# location of satatic files for production
11
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
12
# how Django should look for static file directories; below is default
13
STATICFILES_FINDERS = [
14
# defaults
15
"django.contrib.staticfiles.finders.FileSystemFinder",
16
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
17
]
18
# This gives me a 500 error
19
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
20
urls.py
JavaScript
1
4
1
urlpatterns here
2
3
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
4
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.