【问题标题】:Django Compressor not working in productionDjango Compressor 无法在生产中工作
【发布时间】:2015-05-10 16:34:23
【问题描述】:

我想使用 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文件将模板目录放到app上。

{% 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>

【问题讨论】:

    标签: python django django-compressor


    【解决方案1】:

    将此添加到settings.py

    COMPRESS_OFFLINE = True
    

    并压缩

    python manage.py compress
    

    【讨论】:

    • 没有离线压缩怎么办? COMPRESS_OFFLINE = True 和 COMPRESS_ENABLED = True 对我不起作用。我必须运行离线压缩看起来像。我必须添加 COMPRESS_OFFLINE_CONTEXT 才能在动态上下文中正常运行。
    【解决方案2】:

    快速浏览文档即可找到答案: http://django-compressor.readthedocs.org/en/latest/settings/

    django.conf.settings.COMPRESS_ENABLED 应该设置为 True 否则它与 DEBUG 相反

    更新

    抱歉,这很尴尬,我完全错过了您设置中的:COMPRESS_ENABLED = True

    【讨论】:

    • 我试过这个。 “COMPRESS_ENABLED = 不是调试”。还是不行。
    • 您可以检查的一件事是收集静态日志以查看压缩是否实际发生
    • 我试过了。 python manage.py collectstatic。并且,将 1 个静态文件复制到“/opt/webapps/src/com.app.static”。但是不知道有没有压缩。
    • 复制的文件没有被压缩。只是css文件。
    • 嗨,我试过“python manage.py compress”。之后,那个工作!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2021-07-08
    • 2013-05-02
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多