2015-06-12 96 views
0

错误: 'https://socialboard-in.s3.amazonaws.com/static/notices/index1.css' 不是通过COMPRESS_URL( 'http://socialboard-in.s3.amazonaws.com/')访问,不能被压缩Django的压缩机不工作的托管静态文件在Amazon S3上

settings.py

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'notices', 
    'colleges', 
    'accounts', 
    'follows', 
    'pagination', 
    'topic', 
    'wysihtml5', 
    'crispy_forms', 
    'ratelimit', 
    'compressor', 
    'storages', 
) 
STATIC_PATH = os.path.join(PROJECT_PATH,'static') 
STATIC_ROOT = 'staticfiles' 
STATIC_URL = '/static/' 
STATICFILES_DIRS = (
    STATIC_PATH, 
) 

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    # other finders.. 
    'compressor.finders.CompressorFinder', 
) 


TEMPLATE_DIRS = (
    # Put strings here, like "home/html/django_templates" Or "C:/www/django/templates". 
    # Always use forward slashes, even on windows. 
    # Don't forget to use absolute paths, not relative paths. 
    TEMPLATE_PATH, 
    os.path.join(BASE_DIR,'templates'), 
) 

AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"] 
AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"] 
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME'] 


# All files uploaded to AWS S3 will have very long cache headers 
# automatically. 

AWS_HEADERS = { 
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT', 
'Cache-Control': 'max-age=94608000', 
} 

STATICFILES_STORAGE = 'custom_storages.CachedS3BotoStorage' 

COMPRESS_ENABLED = True 

COMPRESS_URL = "http://socialboard-in.s3.amazonaws.com/" 

STATIC_URL = "http://socialboard-in.s3.amazonaws.com/" 


COMPRESS_STORAGE = 'custom_storages.CachedS3BotoStorage' 
AWS_S3_CUSTOM_DOMAIN = 'socialboard-in.amazonaws.com/' 

custom_storages.py

from django.core.files.storage import get_storage_class 
from storages.backends.s3boto import S3BotoStorage 

class CachedS3BotoStorage(S3BotoStorage): 
    """ 
    S3 storage backend that saves the files locally, too. 
    """ 
    def __init__(self, *args, **kwargs): 
     super(CachedS3BotoStorage, self).__init__(*args, **kwargs) 
     self.local_storage = get_storage_class(
      "compressor.storage.CompressorFileStorage")() 

    def save(self, name, content): 
     name = super(CachedS3BotoStorage, self).save(name, content) 
     self.local_storage._save(name, content) 
     return name 

base.html文件

{% compress css %} 
<link rel="stylesheet" type="text/css" href="{% static 'notices/index1.css' %}" /> 
<link href="{% static 'bootstrap-3.2.0-dist/css/bootstrap.min.css' %}" rel="stylesheet"> 
<!--<link href="{% static 'bootstrap3-wysihtml5.min.css' %}" rel="stylesheet"> --> 
{% block css %}{% endblock %} 
    {% endcompress %} 

一切都没有{%压缩%}标签可以正常使用。

+0

嗨@Sinu Poonia你有没有解决这个问题?我得到完全相同的问题。 –

+0

没有joao。我无法弄清楚。 –

回答

0

我想你需要定义一个临时本地存储为STATIC_ROOT,然后设置COMPRESS_ROOT = STATIC_ROOT,也COMPRESS_STORAGE = STATICFILES_STORAGE

最后在您的自定义缓存s3存储中,它也会在本地缓存文件,发出一个collectstatic(它会在上传到s3之前将其本地推送到STATIC_ROOT),然后希望压缩器能为您工作。

+0

但STATIC_ROOT已被定义为'staticfiles'? –

+0

好了,所以只需设置'COMPRESS_ROOT = STATIC_ROOT' – fpghost

+0

您需要确保在这个目录上的权限是正确的,并执行collectstatic。 Compressor使用该目录中找到的任何内容来执行动态压缩。 – fpghost

相关问题