最新的django_compressor和之前版本的配置方式稍有差别,记录在此以免再走弯路。
安装
代码语言:javascript复制pip install django_compressor
配置
所有配置均是在settings.py中完成
- INSTALLED_APPS
INSTALLED_APPS = [ '..........', 'compressor' ]
- COMPRESS_ROOT
COMPRESS_ROOT = os.path.join(BASE_DIR, "static")
- COMPRESS_ENABLED
COMPRESS_ENABLED = True
- STATICFILES_FINDERS
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
'compressor.finders.CompressorFinder')
使用
首先在html文件中引入标签库 引入标签库
代码语言:javascript复制{% load compress %}
代码语言:javascript复制 {% 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 %}