问题
Trying to deploy a Django application to Heroku using whitenoise and django-compressor.
Deploying it to production with DEBUG = False
and COMPRESS_ENABLED = True
, all my static assets can be accessed without a problem. However, all the compressed files return a 404, e.g.:
http://*.herokuapp.com/static/CACHE/css/fbfaa35dc638.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
Either enabling DEBUG
or disabling COMPRESS_ENABLED
fixes the problem, but of course is not what I want.
I'm also setting STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
, but changing this doesn't help.
Some settings (note I have a settings directory with e.g. base.py
, local.py
, etc. that's why I need an extra ../
on the paths):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '../staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '../static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
In my base template:
{% compress css %}
<link rel="stylesheet" href="{% static 'css/app.css' %}">
{% block css %}{% endblock %}
{% endcompress %}
[...]
{% compress js %}
<script src="{% static 'js/main.js' %}"></script>
{% block js %}{% endblock js %}
{% endcompress %}
Again, moving them out of the compress blocks fixes the issue. Just the compressed files can't be found.
Any ideas?
EDIT
I forgot to mention one setting I added as per the deployment checklist, namely this: https://docs.djangoproject.com/en/1.8/ref/templates/api/#django.template.loaders.cached.Loader
TEMPLATES[0]['OPTIONS']['loaders'] = [
(
'django.template.loaders.cached.Loader', [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
),
]
Removing this setting makes the page work again. HOWEVER, the JS and CSS files are not compressed... What's going on?
Edit 2
This is not a duplicate of Django staticfiles not found on Heroku (with whitenoise) :
- The problem in my question arises from django-compressor, not whitenoise alone
- They're not getting 404s, but rather 500s.
- Their issue was that they forgot to run collectstatic... Which is not the case here.
回答1:
This seems to be a known problem.
According to Squebe - "Compressor's Online mode doesn't work on Heroku because Whitenoise only checks the static folder when the app is loaded (see my comment on issue #680). To use compressor in production I think you need to use it in Offline mode and run python manage.py compress before the app loads using Heroku's post compile hook."
No way to get this to work on Heroku #486
来源:https://stackoverflow.com/questions/33105440/whitenoise-and-django-compressor-cause-404-for-compressed-files