如何配置Django的COM pressor和Django的staticfiles与Amazon的S3? [英] How to configure django-compressor and django-staticfiles with Amazon's S3?

查看:229
本文介绍了如何配置Django的COM pressor和Django的staticfiles与Amazon的S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安装 Django的COM pressor ​​和的 Django的staticfiles 使pssed CSS / Javascript和图像融为一体$ P $供应时间从Amazon的S3

I'm trying to setup django-compressor and django-staticfiles so that the compressed CSS/Javascript and images are served from Amazon's S3.

我已经成功地使用S3作为后端设置staticfiles所以它的 collectstatic 命令将文件发送到S3,而不是 STATIC_ROOT

I've managed to setup staticfiles using S3 as the backend so it's collectstatic command sends the files to S3 instead of STATIC_ROOT.

但是尝试添加的Django-COM pressor ​​进来的时候就是这一切似乎分崩离析我。继上设置远程存储器的文档我创建的一个子类存储后端,博托,所以我复制的<一个href="http://readthedocs.org/docs/django_com$p$pssor/en/latest/remote-storages/#using-staticfiles">example以 storage.py 。当我开始使用这个缓存后端的文件复制到static_media而不是S3。第一页之后加载缓存文件夹出现在S3上,并在static_media文件夹。

However when trying to add django-compressor to the mix is where it all seems to fall apart for me. Following the documentation on setting up remote storages I've created a subclass of the storage backend, boto, so I copied the example to storage.py. Once I start using this cached backend the files are copied into static_media and not S3. After the first page load the CACHE folder appears on S3 and in the static_media folder.

设置 STATICFILES_STORAG​​E COM preSS_STORAG​​E 返回博托正常S3级( storages.backends.s3boto.S3BotoStorage )的结果在静态的资产被收集到的S3存储和没有static_media文件夹。然而试图重新载入页面引发错误:

Setting STATICFILES_STORAGE and COMPRESS_STORAGE back to boto's normal S3 class (storages.backends.s3boto.S3BotoStorage) results in the static assets being collected into the S3 bucket and no static_media folder. However trying to reload the page throws the error:

Caught NotImplementedError while rendering: This backend doesn't support absolute paths.

突出 {%COM preSS的CSS%} 作为标记和 COM pressor ​​/ base.py 作为原点。

highlighting {% compress css %} as the tag and compressor/base.py as the origin.

的S3 / staticfiles / COM pressor节我的 settings.py

The s3/staticfiles/compressor section of my 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

所以,我要去哪里错了?我有没有错误配置时,可能使用 CachedS3BotoStorage 自定义存储的东西吗?

So where am I going wrong? Have I mis-configured something when using the CachedS3BotoStorage custom storage maybe?

推荐答案

您的设置看起来是正确的。你应该保持两个 STATICFILES_STORAG​​E COM preSS_STORAG​​E 设置为 storage.CachedS3BotoStorage ,但并没有切换回 storages.backends.s3boto.S3BotoStorage

Your settings look correct. You should keep both STATICFILES_STORAGE and COMPRESS_STORAGE set to storage.CachedS3BotoStorage though and not switch back to storages.backends.s3boto.S3BotoStorage.

根据这个的Django的COM pressor问题,问题出在在collectstatic工艺方法的Django staticfiles保存(使用 shutil.copy2 )。这个问题在新版本的的Django staticfiles ,可以用来代替已更正一个附带的Django 1.3。

According to this django-compressor issue, the problem is with the way django-staticfiles saves during the collectstatic process (using shutil.copy2). This issue has been corrected in the newer version of django-staticfiles, which can be used instead of the one that ships with Django 1.3.

pip install django-staticfiles==dev

而在你的 settings.py ,切换到更新的版本:

And in your settings.py, switch to the updated version:

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',
    #...
)

运行后蟒蛇manage.py collectstatic 再次,无论是从缓存目录的Django-COM pressor和收集staticfiles文件应该出现在S3上。

After running python manage.py collectstatic again, both the CACHE directory from django-compressor and the collected staticfiles files should show up on S3.

这篇关于如何配置Django的COM pressor和Django的staticfiles与Amazon的S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆