Django压缩器无法压缩文件

13

我试图让django-compressor与mezzanine一起使用。 首先,我按照Mezzanine的要求安装了django compressor,并将DEBUG = False更改,但在Django生成的HTML中没有任何变化。 因此,我遵循了django compressor的文档,并修改了我的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

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

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

这里是我在模板中如何使用压缩器的示例:

{% load pages_tags mezzanine_tags i18n future staticfiles compress %}
{% compress css %}
<link rel="stylesheet" href="{% static "css/custom/mycss.css" %}">
{% endcompress %}

没有任何反应,直到我运行了:

python manage.py compress --force

现在我的缓存已经填充,来自Django的HTML指向CACHE中的文件,如下所示:

<link rel="stylesheet" href="/static/CACHE/css/16e8b98f5bd3.css" type="text/css" media="screen">

但是这些文件没有被压缩,Django Compressor只是复制并更改了它们的名称。你知道为什么Compressor没有对它们进行压缩吗?

但是这些文件没有经过压缩,Django Compressor只是复制并更改了它们的名称。您知道为什么Compressor没有对它们进行压缩吗?
2个回答

43

Django compressor即使在DEBUG = False的情况下也无法在Django服务器上运行。默认情况下,它只会将所有CSS文件合并为一个文件。如果要缩小文件等其他操作,可以应用过滤器。以下是我在settings.py中的操作:

COMPRESS_ENABLED = True
COMPRESS_CSS_FILTERS = ['compressor.filters.css_default.CssAbsoluteFilter',  'compressor.filters.cssmin.CSSMinFilter']

我认为这对他人会有所帮助。干杯!


2
问题出在memcached上,禁用后,Django显示了权限问题,要缩小css文件我需要选择一个过滤器,比如compressor.filters.cssmin.CSSMinFilter。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接