【问题标题】:django-compressor CommandError: An error occurred during rendering file/path.html: Invalid class path 'css'django-compressor CommandError:渲染文件/path.html时出错:无效的类路径'css'
【发布时间】:2018-11-27 20:57:16
【问题描述】:

我正在尝试将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',
)

在我项目的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 %}

【问题讨论】:

    标签: django django-compressor


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。那行得通。我必须承认我从来没有真正正确地看待过这个问题。我有一些过滤器定义了旧样式。现在我在文档中发现了新样式,我完全没有更新变量名。太糟糕了,谢谢
    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 2014-05-19
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多