我试图将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 10
,Django==2.0.5
,django-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',
)
在我项目的<head></head>
模板的base.html
标记内,我有
{% 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 %}
答案 0 :(得分:0)
您必须定义COMPRESS_FILTERS
而不是COMPRESS_CSS_FILTERS
,因为后者只是前者css
密钥的向后兼容别名。也就是说,您的配置应为:
COMPRESS_FILTERS = {
'css': ['compressor.filters.css_default.CssAbsoluteFilter'],
'js': ['compressor.filters.jsmin.JSMinFilter']
}