ghi*_*man 29 django amazon-s3 django-staticfiles django-compressor
我正在尝试设置django-compressor和django-staticfiles,以便从亚马逊的S3提供压缩的CSS/Javascript和图像.
我已经设法使用S3作为后端设置静态collectstatic
文件,因此它的命令将文件发送到S3而不是STATIC_ROOT
.
然而,当试图添加django-compressor
到混合中时,它似乎对我来说都是分崩离析的.按照设置远程存储的文档,我创建了存储后端的子类boto,所以我将示例复制到了storage.py
.一旦我开始使用这个缓存的后端,文件就会被复制到static_media而不是S3.第一页加载后,CACHE文件夹出现在S3和static_media文件夹中.
设置STATICFILES_STORAGE
并COMPRESS_STORAGE
返回到boto的普通S3 class(storages.backends.s3boto.S3BotoStorage
)会导致静态资产被收集到S3存储桶中,而不会收集到static_media文件夹.但是,尝试重新加载页面会引发错误:
Caught NotImplementedError while rendering: This backend doesn't support absolute paths.
Run Code Online (Sandbox Code Playgroud)
突出显示{% compress css %}
为标记和compressor/base.py
原点.
我的s3/staticfiles/compress部分settings.py
:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'key'
AWS_SECRET_ACCESS_KEY ='secret'
AWS_STORAGE_BUCKET_NAME = 'my-bucket'
S3_URL = 'http://my-bucket.s3.amazonaws.com/'
MEDIA_ROOT = 'client_media'
MEDIA_URL = '/media/'
STATIC_ROOT = 'static_media'
STATIC_URL = S3_URL
ADMIN_MEDIA_PREFIX = S3_URL + 'admin/'
STATICFILES_DIRS = (
join(DIRNAME, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
COMPRESS_ENABLED = True
COMPRESS_URL = S3_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = 'storage.CachedS3BotoStorage'
STATICFILES_STORAGE = COMPRESS_STORAGE
Run Code Online (Sandbox Code Playgroud)
那我哪里错了?在使用CachedS3BotoStorage
自定义存储时,我是否错误配置了某些内容?
小智 10
您的设置看起来正确.您应该保留两者STATICFILES_STORAGE
并COMPRESS_STORAGE
设置为storage.CachedS3BotoStorage
但不切换回storages.backends.s3boto.S3BotoStorage
.
根据这个 django压缩器问题,问题在于django-staticfiles在collectstatic过程中保存的方式(使用shutil.copy2
).此问题已在较新版本的django-staticfiles中得到纠正,可以使用它来代替Django 1.3附带的版本.
pip install django-staticfiles==dev
Run Code Online (Sandbox Code Playgroud)
在您的settings.py
,切换到更新版本:
STATICFILES_FINDERS = (
#"django.contrib.staticfiles.finders.FileSystemFinder",
#"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"staticfiles.finders.FileSystemFinder",
"staticfiles.finders.AppDirectoriesFinder",
"compressor.finders.CompressorFinder",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.staticfiles',
'staticfiles',
#...
)
Run Code Online (Sandbox Code Playgroud)
python manage.py collectstatic
再次运行后,django-compressor的CACHE目录和收集的静态文件文件都应显示在S3上.