所以我知道這個主題已經有幾篇文章了,但是我已經嘗試了各種設置組合來使django-compressor
工作,但沒有成功。有任何想法嗎?在Heroku和亞馬遜S3上的Django-compressor錯誤「UncompressableFileError」
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 %}
錯誤:
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
所以我從回溯錯誤被提出了這個錯誤認識,因爲django-compressor
使用代碼:
if not url.startswith(base_url):
raise UncompressableFileError(
"'%s' isn't accesible via COMPRESS_URL ('%s') and can't be"
" compressed" % (url, base_url))
如此看來,由於某種原因,COMPRESS_URL
使用:443
端口,是缺少/static/
後綴,否則會起作用。
設置:
- 的Django v1.10.6
- 的Python V3.5
- Django的壓縮機2.1.1