SoFunction
Updated on 2024-11-19

Using Django Compressor, a compression component in the Django framework for Python.

In order to speed up the loading speed of the website, we usually have to compress more js and css. These js and css compression work if all manual processing, time-consuming and laborious.

Django Compressor can realize the automatic compression of js/css. Django Compressor in the ease of use is very good, according to the documentation to do a simple setup will work. Strongly recommend that you go to the document will be a complete look over (the document is very short).

To use Django Compressor, you just need to put the css/js into the compress tag and Django Compressor will process it automatically. In debug mode, Django Compressor will not do any processing. In non-debug mode, Django Compressor will automatically compress the js/css and output the compressed issue to django's STATIC_ROOT directory. So please make sure that the STATIC_ROOT directory is set up correctly.

{% 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/" type="text/css" charset="utf-8">
{% endcompress %}
 

Coffeescript, less support

In the development stage coffeescript and less can be used directly to deal with js, in the official release in the loading speed considerations need to be pre-compiled into js and css. Django Compressor provides COMPRESS_PRECOMPILERS settings, according to the type type of pre-processing.

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}'),
)