Django compressor

码农软件 · 软件分类 · 其他开发相关 · 2019-10-25 09:59:26

软件介绍

为了加快网站的加载速度,我们通常要多js和css进行压缩处理。这些js和css的压缩工作如果都手动处理,费时费力。

Django Compressor 可以实现js/css的自动压缩。Django Compressor在易用性方面做的非常好,按照 文档 做简单的设置后就可以正常工作。强烈建议大家去将文档完整的看一遍(文档很短)。

使用的时候,只需要将css/js放到 compress 标签中 Django Compressor 即可自动进行处理。在debug模式时, Django Compressor 不会对做任何处理。在非debug模式时,Django Compressor会自动对js/css进行压缩,并将压缩后的问题输出到django的 STATIC_ROOT 目录。所以请务必保证 STATIC_ROOT 目录进行了正确的设置。

{% load compress %}
{% compress <js/css> [<file/inline> [block_name]] %}
<html of inline or linked JS/CSS>
{% endcompress %}

{% compress css %}
<link rel="stylesheet" href="/static/css/one.css" type="text/css" charset="utf-8">
{% endcompress %}

coffeescript、less 支持

在开发阶段coffeescript和less可以直接使用js来处理,在正式发布时处于加载速度的考虑需要预先编译成js和css。 Django Compressor 提供 COMPRESS_PRECOMPILERS 设置,根据type类型进行预处理。

COMPRESS_PRECOMPILERS = (
    ('text/coffeescript', 'coffee --compile --stdio'),
    ('text/less', 'lessc {infile} {outfile}'),
    ('text/x-sass', 'sass {infile} {outfile}'),
    ('text/x-scss', 'sass --scss {infile} {outfile}'),
)

为可以在开发阶段正常使用coffeescript和less,在开发阶段需要引入对应的js文件,同时需要在非开发环境自动关闭。遗憾的是Django Compressor并没有提供相关的设置。为此我写了个在模板中获取django settings设置的tagget_setting。该tag包含在我的另一个项目 django-helper 中。

 {% get_setting "COMPRESS_ENABLED" "" "COMPRESS_ENABLED" %}
    {% if not COMPRESS_ENABLED %}
    <script src="{{STATIC_URL}}js/coffee-script.js"></script>
    {% endif %}

本文地址:https://codercto.com/soft/d/17516.html

Numerical Recipes 3rd Edition

Numerical Recipes 3rd Edition

William H. Press、Saul A. Teukolsky、William T. Vetterling、Brian P. Flannery / Cambridge University Press / 2007-9-6 / GBP 64.99

Do you want easy access to the latest methods in scientific computing? This greatly expanded third edition of Numerical Recipes has it, with wider coverage than ever before, many new, expanded and upd......一起来看看 《Numerical Recipes 3rd Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具