Django压缩器在生产环境中无法工作

5

我希望使用django_compressor,但在我的生产环境中无法工作。

在开发环境中(DEBUG=True),它可以运行并创建 .sass-cacheCACHE 文件夹。

我的 settings.py 如下:

DEBUG = False
TEMPLATE_DEBUG = False
INSTALLED_APPS = (
    ...,
    'django.contrib.staticfiles',
    'compressor',
    'myapp',
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'com.app.static')↲
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)
COMPRESS_ENABLED = True
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_PRECOMPILERS = (
    ('text/x-scss', 'sass --scss {infile} {outfile}'),
)
MEDIA_URL = '/media/'

将scss文件放在应用程序的模板目录中。

{% load staticfiles %}
{% load compress %}
<html>
<head>
    {% compress css %}
        <link rel='stylesheet' type='text/scss' href="{% static 'top/css/top.scss' %}" charset='utf-8'>
    {% endcompress %}
</head>
</html>
2个回答

6
将以下内容添加到settings.py文件中。
COMPRESS_OFFLINE = True

并压缩

python manage.py compress

1
你如何在不使用离线压缩的情况下实现它?对我来说,启用 COMPRESS_OFFLINE = True 和 COMPRESS_ENABLED = True 并不能正常工作。看起来我必须运行离线压缩。我不得不添加 COMPRESS_OFFLINE_CONTEXT 才能正确地在动态上下文中运行。 - radtek

0

我尝试了这个。"COMPRESS_ENABLED= not DEBUG"。仍然不起作用。 - Hiro3
你可以检查collectstatic日志,看看是否实际进行了压缩。 - Fanis Despoudis
我试过了。 python manage.py collectstatic。 还有, 1个静态文件被复制到'/opt/webapps/src/com.app.static'。但是我不知道是否已经压缩。 - Hiro3
复制的文件没有压缩,只是 CSS 文件。 - Hiro3
嗨,我尝试了“python manage.py compress”。之后,它工作了!谢谢。 - Hiro3

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