2017-03-08 67 views
1

所以我知道這個主題已經有幾篇文章了,但是我已經嘗試了各種設置組合來使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

回答

0

docs,它看起來像你應該設置:

COMPRESS_URL = "http://mysite.s3.amazonaws.com/" 

而不是部分網址就像你的問題所示。但我不確定這是你的問題,因爲你正在使用的設置。 (我的理解是要壓縮到S3獲取文件和本地存儲壓縮的結果)

看着django source code for Storage class,我會檢查你有什麼對你的設置MEDIA_URL,這可能是其中端口443是從。

如果這沒有幫助,我建議您發佈錯誤的完整追溯,這可能有助於理解正在發生的事情。