使用django-compressor
压缩混淆静态文件
django-compressor
可以把js/css
等静态文件压缩,这样有利于减少网站的请求次数,还能节省网络带宽。
下面介绍下如何在django
中集成django-compressor
安装django-compressor
安装很简单,pip
安装下就可以了:
pip install django-compressor
然后在'setting'的INSTALLED_APPS
中添加
INSTALLED_APPS = [
#other
'compressor'
]
setting配置
首先确保django.contrib.staticfiles
已经包含在INSTALLED_APPS
中,django1.6及以上版本是默认包含该app在其中的.
指定STATIC_URL
STATIC_ROOT = os.path.join(SITE_ROOT, 'collectedstatic')
STATIC_URL = '/static/'
STATICFILES = os.path.join(BASE_DIR, 'static')
STATIC_URL是客户端访问静态资源的根路径
配置STATICFILES_FINDERS
:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#other
'compressor.finders.CompressorFinder',
)
添加django-compressor
配置:
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_CSS_FILTERS = [
#creates absolute urls from relative ones
'compressor.filters.css_default.CssAbsoluteFilter',
#css minimizer
'compressor.filters.cssmin.CSSMinFilter'
]
COMPRESS_JS_FILTERS = [
'compressor.filters.jsmin.JSMinFilter'
]
使用
使用也很简单,如下:
{% load compress %}
{% compress css %}
<link rel='stylesheet' href='{% static 'blog/css/style.css' %}' type='text/css'/>
{% endcompress %}
{% compress js %}
<script type="text/javascript" src="{% static 'blog/js/jquery-3.1.1.js' %}"></script>
{% endcompress %}
分别是css和js的使用方式
您好,我pip install django-compressor时总是报错。 c:\users\supers\appdata\local\programs\python\python36\include\pyconfig.h(59): fatal error C1083: 无法打开包括文 件: “io.h”: No such file or directory error: command 'D:\vs2019\VC\Tools\MSVC\14.20.27508\bin\HostX86\x64\cl.exe' failed with exit status 2
good
of course.
hzhzh
fsdfsf
good