django-compressor CommandError: An error occurred during rendering file/path.html: Invalid class path 'css'

django-compressor CommandError: An error occurred during rendering file/path.html: Invalid class path 'css'

我正尝试将 django-compressor 用于我的项目。到目前为止,在我的开发环境中,当我 运行 python manage.py compress 我得到这个错误 CommandError: An error occurred during rendering D:path/to/a/template.html: Invalid class path 'css'

我有很多模板文件分布在许多应用程序中。错误随机弹出,每次我 运行 python manage.py compress.

指向不同的模板

我也先试过运行宁python manage.py collectstatic,但无济于事而且docs中似乎没有提到这个错误

OS: Windows 10Django==2.0.5django-compressor==2.2

下面的相关 settings.py 部分

DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

COMPRESS_ROOT = STATIC_ROOT
COMPRESS_URL = STATIC_URL
COMPRESS_PARSER = 'compressor.parser.HtmlParser'
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_OFFLINE_MANIFEST = 'compressor_manifest.json'
COMPRESS_CSS_FILTERS = {
    'css': ['compressor.filters.css_default.CssAbsoluteFilter'], 
    'js': ['compressor.filters.jsmin.JSMinFilter']
}
COMPRESS_PRECOMPILERS = (
    ('text/x-scss', 'django_libsass.SassCompiler'),
)

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

在我项目的 base.html 模板的 <head></head> 标签内

{% load compress %}
{% load staticfiles %}
{% load static %}

{% compress css file %}
<link rel="stylesheet" href="{% static 'css/fname.css' %}">
{% endcompress %}

{% compress js file %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'js/fname.js' %}"></script>
{% endcompress %}

您必须定义 COMPRESS_FILTERS 而不是 COMPRESS_CSS_FILTERS,因为后者只是前者的 css 键的向后兼容别名。也就是说,您的配置应为:

COMPRESS_FILTERS = {
    'css': ['compressor.filters.css_default.CssAbsoluteFilter'], 
    'js': ['compressor.filters.jsmin.JSMinFilter']
}