标签: django-compressor

如何使用亚马逊的S3配置django-compressor和django-staticfiles?

我正在尝试设置django-compressordjango-staticfiles,以便从亚马逊的S3提供压缩的CSS/Javascript和图像.

我已经设法使用S3作为后端设置静态collectstatic文件,因此它的命令将文件发送到S3而不是STATIC_ROOT.

然而,当试图添加django-compressor到混合中时,它似乎对我来说都是分崩离析的.按照设置远程存储的文档,我创建了存储后端的子类boto,所以我将示例复制到了storage.py.一旦我开始使用这个缓存的后端,文件就会被复制到static_media而不是S3.第一页加载后,CACHE文件夹出现在S3和static_media文件夹中.

设置STATICFILES_STORAGECOMPRESS_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 + …
Run Code Online (Sandbox Code Playgroud)

django amazon-s3 django-staticfiles django-compressor

29
推荐指数
1
解决办法
9529
查看次数

Django-Compressor抛出UncompressableFileError

我正在使用django-compressor和django-staticfiles(外部版本,我在Django 1.2上).

当我尝试加载我的网站时,我收到一个错误:

TemplateSyntaxError: Caught UncompressableFileError while rendering: 'css/facebox.css' isn't accesible via COMPRESS_URL ('/static/') and can't be compressed
Run Code Online (Sandbox Code Playgroud)

我已经验证COMPRESS_URL等于STATIC_URL,并且该文件实际上可以通过该URL访问.

看看django-compressor代码,我发现抛出异常的地方:

 def get_basename(self, url):
        try:
            base_url = self.storage.base_url
        except AttributeError:
            base_url = settings.COMPRESS_URL
        # I added the following print statement:
        print "url: %s, base_url: %s" % (url, base_url)
        if not url.startswith(base_url):
            raise UncompressableFileError("'%s' isn't accesible via "
                                          "COMPRESS_URL ('%s') and can't be "
                                          "compressed" % (url, base_url))
Run Code Online (Sandbox Code Playgroud)

{% compress css %}我的模板中的第一个块是这样的:

{% compress css %}
<link …
Run Code Online (Sandbox Code Playgroud)

django django-staticfiles django-compressor

23
推荐指数
1
解决办法
1万
查看次数

Django-compressor:如何写入S3,从CloudFront读取?

我想从CloudFront提供我的压缩CSS/JS(他们生活在S3上),但我无法通过settings.py中的压缩器设置来解决这个问题,我有以下内容:

    COMPRESS_OFFLINE = True 
    COMPRESS_URL = 'http://static.example.com/' #same as STATIC_URL, so unnecessary, just here for simplicity
    COMPRESS_STORAGE = 'my_example_dir.storage.CachedS3BotoStorage' #subclass suggested in [docs][1]
    COMPRESS_OUTPUT_DIR = 'compressed_static'
    COMPRESS_ROOT = '/home/dotcloud/current/static/' #location of static files on server
Run Code Online (Sandbox Code Playgroud)

尽管有COMPRESS_URL,我的文件正在从我的s3存储桶中读取:
<link rel="stylesheet" href="https://example.s3.amazonaws.com/compressed_static/css/e0684a1d5c25.css?Signature=blahblahblah;Expires=farfuture;AWSAccessKeyId=blahblahblah" type="text/css" />

我想问题是我想将文件写入S3,但是从CloudFront读取它.这可能吗?

python django amazon-s3 amazon-cloudfront django-compressor

17
推荐指数
2
解决办法
7520
查看次数

django-compressor没有在Heroku上设置绝对CSS图像路径

我正在使用django-compressor来连接和压缩我在这个站点上的CSS和JS文件.我正在从S3存储桶提供静态文件.

在我的本地网站副本上,使用不同的S3存储桶,这一切都很完美.但是在Heroku上托管的实时网站上,除了 CSS文件中图像的相对URL不会被重写之外,它都可以工作.

例如,CSS文件中的这一行:

background-image: url("../img/glyphicons-halflings-grey.png");
Run Code Online (Sandbox Code Playgroud)

被重写为:

background-image:url('https://my-dev-bucket-name.s3.amazonaws.com/static/img/glyphicons-halflings-grey.png')
Run Code Online (Sandbox Code Playgroud)

在我的开发网站上,但没有触及现场网站.因此,实时站点最终会查找pepysdiary.s3.amazonaws.com/static/CACHE/img/图像(因为它与新的压缩CSS文件相关).

现在,我已经在包含图像的位置放了一个目录,但我无法弄清楚为什么会有这种差异.两个网站都在他们的设置中有这个:

COMPRESS_CSS_FILTERS = [
    # Creates absolute urls from relative ones.
    'compressor.filters.css_default.CssAbsoluteFilter',
    # CSS minimizer.
    'compressor.filters.cssmin.CSSMinFilter'
]
Run Code Online (Sandbox Code Playgroud)

并且CSS文件正在被最小化......但是就像其他过滤器没有在实际站点上应用.

django amazon-s3 django-compressor

16
推荐指数
1
解决办法
2331
查看次数

Python/Tornado - 压缩静态文件

对于django项目,有一个很棒的工具叫做django-compressor.它将compress模板标记下的所有js或css文件组合成单个缓存文件,如下所示:

{% load compress %}

{% compress css %}
<link rel="stylesheet" href="/static/css/one.css" type="text/css" charset="utf-8">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" charset="utf-8">
{% endcompress %}
Run Code Online (Sandbox Code Playgroud)

我想知道龙卷风项目是否有这样的东西?或者也许是任何变通方法/替代方案?

我在github上找到了这个项目,但它已经不再维护了.

python django templates tornado django-compressor

14
推荐指数
1
解决办法
1710
查看次数

如何在heroku上设置django-compressor,离线压缩到S3

我按照在SO和不同博客中找到的每个QA建议,在我的开发机器上一切正常,在heroku上没有任何作用.

这是我的设置:

DEFAULT_FILE_STORAGE = 'arena.utils.MediaRootS3BotoStorage' # media files
# storage

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_PRELOAD_METADATA = True # necessary to fix manage.py collectstatic command to only upload changed files instead of all files

S3_URL = 'https://%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = S3_URL + '/media/'

STATIC_URL = S3_URL + '/static/'

ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'


COMPRESS_URL = STATIC_URL
COMPRESS_OFFLINE = True
COMPRESS_STORAGE = 'utils.CachedS3BotoStorage'
STATICFILES_STORAGE = COMPRESS_STORAGE
Run Code Online (Sandbox Code Playgroud)

当我运行collectstatic/compress时,一切正常,我看到文件被收集到S3并放入适当的位置.我看到了清单文件.

加载任何带有压缩标签的页面,显示错误再次OfflineGenerationError: You have offline compression enabled but key …

django heroku amazon-s3 django-compressor

14
推荐指数
1
解决办法
3186
查看次数

使用带有Compressor的Boto的Django AWS S3无法压缩UncompressableFileError

根据本指南和这些[1] [2]帖子,我尝试使用django-storages Boto在AWS S3上设置静态存储.

执行时collectstatic,命令成功收集STATIC_ROOT.但是,文件未上传到S3压缩,并且服务器无法提供服务.返回500错误.看日志:

错误信息:

UncompressableFileError: 'https://<myapp>.s3.amazonaws.com/static/oscar/css/styles.css' could not be found in the COMPRESS_ROOT '/var/www/<myappname>/static' or with staticfiles.
Run Code Online (Sandbox Code Playgroud)

编辑:

我还将STATIC_URL更改为http://%s/ % AWS_S3_CUSTOM_DOMAIN,并且我得到了相同的错误,除了它仍在搜索https,但COMPRESS_URL是http.

UncompressableFileError: 'https://<myappname>.s3.amazonaws.com/static/oscar/css/styles.css' isn't accessible via COMPRESS_URL ('http://<myappname>.s3.amazonaws.com/') and can't be compressed
Run Code Online (Sandbox Code Playgroud)

这与压缩机和Boto不兼容吗?

相关代码:

# settings/prod.py

AWS_ACCESS_KEY_ID = <Key_ID>
AWS_SECRET_ACCESS_KEY = <Secret_Key>
AWS_STORAGE_BUCKET_NAME = "<my_bucket_name>"
AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
AWS_LOCATION = 'static'

DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"
STATICFILES_STORAGE = "myapp.storage.s3utils.CachedS3BotoStorage" …
Run Code Online (Sandbox Code Playgroud)

django amazon-s3 boto django-storage django-compressor

14
推荐指数
1
解决办法
1043
查看次数

Django Compressor不会缩小文件

我想让django压缩机与夹层一起工作.首次尝试我只是安装了django压缩器(对于Mezzanine应该这样做)并且更改了DEBUG = False但是从Django生成的HTML没有任何改变.所以我按照django压缩器的文档修改了我的settings.py:

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

INSTALLED_APPS = (
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.redirects",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.sitemaps",
    "django.contrib.staticfiles",
    "mezzanine.boot",
    "mezzanine.conf",
    "mezzanine.core",
    "mezzanine.generic",
    "mezzanine.blog",
    "mezzanine.forms",
    "mezzanine.pages",
    "mezzanine.galleries",
    "mezzanine.twitter",
    #"mezzanine.accounts",
    #"mezzanine.mobile",
    #'debug_toolbar',
    "compressor",
)

OPTIONAL_APPS = (
    #"debug_toolbar",
    "django_extensions",
    #"compressor", I commented it to follow the django-compressor doc
    PACKAGE_NAME_FILEBROWSER,
    PACKAGE_NAME_GRAPPELLI,
)

COMPRESS_ENABLED = True
COMPRESS_ROOT = STATIC_ROOT
Run Code Online (Sandbox Code Playgroud)

这些是我环境中安装的软件包:

Django==1.6.5
Mezzanine==3.1.5
Pillow==2.5.1
bleach==1.4
distribute==0.6.24
django-appconf==0.6
django-compressor==1.4
filebrowser-safe==0.3.5
future==0.9.0
grappelli-safe==0.3.12
html5lib==1.0b3
oauthlib==0.6.3
pytz==2014.4
requests==2.3.0
requests-oauthlib==0.4.1
six==1.7.3
tzlocal==1.0
Run Code Online (Sandbox Code Playgroud)

在这里,我如何在模板中使用压缩器:

{% load …
Run Code Online (Sandbox Code Playgroud)

python django django-compressor mezzanine

12
推荐指数
2
解决办法
5819
查看次数

如何在负载均衡器后面使用django-compressor?

我在负载均衡器后面有两台服务器.每个服务器都运行一个memcached服务器,并且设置文件(两个服务器上都相同)都定义了它们(简而言之:共享缓存).

我希望生成的文件的路径在服务器上是相同的,这样客户端就不必多次下载.

为了让我能够正常工作,我需要了解django压缩机的工作原理.

  • django压缩器中缓存的实际目的是什么?
  • 文件内容是存储在缓存还是文件系统中?
    • 如果是这样,首先会发生什么?
  • 我希望我在这里提出正确的问题.随意添加一些.

更详细和更好的构造序列将是非常有帮助的.

编辑

  • 由于服务器都共享一个memcached服务器,我应该设置COMPRESS_CACHE_KEY_FUNCTION = 'compressor.cache.socket_cachekey'(参见开发分支)还是使用相同的缓存密钥有助于我拥有相同的文件名?
  • 我理解这一点的方式是从源js/css文件中收集mtime以确定它们是否已经改变并且应该从它们生成新文件.正确?
    • 这可能不会发生在每个负载上.它什么时候发生?

python django django-compressor

11
推荐指数
2
解决办法
2392
查看次数

Django压缩错误:输入类型无效:'CacheKey'

在压缩生产服务器上的django静态文件时,我们突然开始遇到此问题.Ubuntu 16.04,Python 3.x,Django 1.11.我正在使用ansible-playbook进行部署.

错误如下:

CommandError: An error occurred during rendering /chalktalk/app/chalktalk-react-40/chalktalk-react-40/chalktalk/apps/exams/templates/exams/section-edit.html: Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.

它似乎不是一个静态文件中的问题,而是一个普遍的问题.每次运行它时,我们都会得到一个不同的文件.

我正在寻找谷歌上的任何线索,并没有出现相同的错误.

python django django-compressor ansible

10
推荐指数
1
解决办法
1962
查看次数