Django Compressor不压缩文件
我正在尝试让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
接下来,我在模板中使用了compressor:
{% 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只是复制了它们并改了名字。你知道为什么压缩器没有对它们进行压缩吗?
2 个回答
43
Django压缩工具在django服务器上即使设置了DEBUG = False
也不会运行。默认情况下,它只会把你所有的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。