Use django-compressor to accelerate static files

Use django-compressor to accelerate static files

Use django-compressor to accelerate static files

Get started

django-compressor By compressing the code jscss to play the effect of accelerating the site. It can also merge multiple static files into one, which not only reduces the number of website requests, but also saves network bandwidth.

installation

It can pip be installed:

pip install django_compressor

In Django's settings.py add:

INSTALLED_APPS = ( 
# other apps 
"compressor", 

If the configuration of django staticfiles, you must django-compressor file finder added to the STATICFILES_FINDERS settings:

STATICFILES_FINDERS = ( 
'django.contrib.staticfiles.finders.FileSystemFinder', 
'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# other 
'compressor.finders.CompressorFinder', )

django-compressor Turn depends on DEBUG the parameters, the default COMPRESS_ENABLED and DEBUG the opposite values. Because the django-compressor function itself is used in a production environment before the project released the static file compression process. 

Therefore, if you want DEBUG=True to test and use it during the development phase ( ),

you need to set it manually COMPRESS_ENABLED=True. But the latest version to change the COMPRESS_OFFLINE = True .

use

The format used in the template is as follows:


Example :

{% compress css %} 
<link rel="stylesheet" href="/static/css/one.css" type="text/css" charset="utf-8"> 
<style type="text/css">
p { border:5px solid green;}</style> 
<link rel="stylesheet" href="/static/css/two.css" type="text/css" charset="utf-8"> 
{% endcompress %}
 
<link rel="stylesheet" href="/static/CACHE/css/output.f7c661b7a124.css" type="text/css" charset="utf-8">

Add in line parameters will be placed directly into the content rendered page instead of file:

{% compress js inline %} 
<script src="/static/js/one.js" type="text/javascript" charset="utf-8"></script> 
<script type="text/javascript" charset="utf-8">obj.value = "value";</script> 
{% endcompress %}
 
<script type="text/javascript" charset="utf-8">
obj = {}; 
obj.value = "value"; 
</script>

In block_name can specify the output file name, for example:

{% compress js file base %} 
<script src="/static/js/one.js" type="text/javascript" charset="utf-8"></script> 
<script type="text/javascript" charset="utf-8">obj.value = "value";</script> 
{% endcompress %} 
<script type="text/javascript" src="/static/CACHE/js/base.3f33b9146e12.js" charset="utf-8"></script>

compression

Generate compressed files is done by the command, so each modification js, css only, will need to reload the latest file to STATIC_ROOT the directory continues:

python manage.py collectstatic 
python manage.py compress

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0