So I know there are a few articles on this subject already, but I have tried every combination of settings to make django-compressor
work, but with no success. Any ideas?
settings.py
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# other finders..
'compressor.finders.CompressorFinder',
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATIC_URL = '/static/'
DEFAULT_FILE_STORAGE = "mysite.s3utils.MediaS3BotoStorage"
COMPRESS_ROOT = STATIC_ROOT
STATICFILES_STORAGE = 'mysite.storage.CachedS3BotoStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE
COMPRESS_URL = STATIC_URL
s3utils.py
from django.core.files.storage import get_storage_class
from storages.backends.s3boto import S3BotoStorage
class StaticS3BotoStorage(S3BotoStorage):
"""
Storage for static files.
"""
def __init__(self, *args, **kwargs):
kwargs['location'] = 'static'
super().__init__(*args, **kwargs)
class MediaS3BotoStorage(S3BotoStorage):
"""
Storage for uploaded media files.
"""
def __init__(self, *args, **kwargs):
kwargs['location'] = 'media'
super().__init__(*args, **kwargs)
class CachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that saves the files locally, too.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.local_storage = get_storage_class(
"compressor.storage.CompressorFileStorage")()
def save(self, name, content):
self.local_storage._save(name, content)
super().save(name, self.local_storage._open(name))
return name
index.html
{% load compress %}
{% compress css %}
<link href="{% static 'libs/twitter-bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
{% endcompress %}
Error:
compressor.exceptions.UncompressableFileError: 'https://mysite.s3.amazonaws.com:443/libs/twitter-bootstrap/css/bootstrap.min.css' isn't accessible via COMPRESS_URL ('https://mysite.s3.amazonaws.com/static/') and can't be compressed
So I know from the traceback error that this error is being raised because django-compressor
uses the code:
if not url.startswith(base_url):
raise UncompressableFileError(
"'%s' isn't accesible via COMPRESS_URL ('%s') and can't be"
" compressed" % (url, base_url))
so it seems that for some reason the COMPRESS_URL
is using the :443
port, and is missing the /static/
suffix, otherwise it would be working.
Setup:
- Django v1.10.6
- Python v3.5
- Django-compressor 2.1.1