django 配置django_compressor 2.3 压缩js和css
最新的django_compressor和之前版本的配置方式稍有差别,记录在此以免再走弯路。
安装
pip install django_compressor
pip install django_compressor
配置
所有配置均是在settings.py中完成
- INSTALLED_APPS
INSTALLED_APPS = [
'..........',
'compressor'
]
INSTALLED_APPS = [
'..........',
'compressor'
]
- COMPRESS_ROOT
COMPRESS_ROOT = os.path.join(BASE_DIR, "static")
COMPRESS_ROOT = os.path.join(BASE_DIR, "static")
- COMPRESS_ENABLED
COMPRESS_ENABLED = True
COMPRESS_ENABLED = True
- STATICFILES_FINDERS
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
'compressor.finders.CompressorFinder')
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
'compressor.finders.CompressorFinder')
使用
首先在html文件中引入标签库
- 引入标签库
{% load compress %}
{% load compress %}
{% compress js %}
<script type="text/javascript" charset="utf-8" src="{% static 'js/a.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/b.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/c.js' %}"></script>
{% endcompress %}
{% compress js %}
<script type="text/javascript" charset="utf-8" src="{% static 'js/a.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/b.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/c.js' %}"></script>
{% endcompress %}