Home > Web Front-end > JS Tutorial > body text

Django's js/css compression component Django Compressor

高洛峰
Release: 2016-11-24 09:31:55
Original
1591 people have browsed it

In order to speed up the loading speed of the website, we usually need to compress more js and css. If all these js and css compression tasks are handled manually, it will be time-consuming and laborious.

Django Compressor can realize automatic compression of js/css. Django Compressor does a very good job in terms of ease of use. It can work normally after simple settings according to the documentation. It is strongly recommended that everyone read the document in its entirety (the document is very short).

When using it, you only need to put the css/js in the compress tag and Django Compressor can automatically process it. In debug mode, Django Compressor will not do anything. In non-debug mode, Django Compressor will automatically compress js/css and output the compressed issues to Django's STATIC_ROOT directory. So please make sure that the STATIC_ROOT directory is set correctly.

{% load compress %}
{% compress [ [block_name]] %}

{% endcompress %}

{% compress css %}
{% endcompress %}

coffeescript, less supports

coffeescript and Less can be processed directly using js. When officially released, it needs to be pre-compiled into js and css for the sake of loading speed. Django Compressor provides the COMPRESS_PRECOMPILERS setting to perform preprocessing based on 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}'),
)

is that coffeescript can be used normally during the development stage and less, the corresponding js file needs to be introduced during the development stage, and needs to be automatically closed in the non-development environment. Unfortunately, Django Compressor does not provide related settings. For this purpose, I wrote a tagget_setting that gets the django settings in the template. This tag is included in another project of mine, django-helper.

{% get_setting "COMPRESS_ENABLED" "" "COMPRESS_ENABLED" %}
  {% if not COMPRESS_ENABLED %}

{% endif %}


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!