使用django-pipline 静态管理器 和 django-compress压缩工具

需要先安装两个包并且在django中配置好

安装(pipeline>1.4版本 和  django-compress 为2.2版本)

pip  install django-pipeline

在INSTALLED_APPS中添加 piplene,

INSTALLED_APPS  =  (
    'pipeline' ,

 STATICFILES_STORAG添加

STATICFILES_STORAGE  =  'pipeline.storage.PipelineCachedStorage'

添加PipelineFinder到STATICFILES_FINDERS

STATICFILES_FINDERS  =  (
    'django.contrib.staticfiles.finders.FileSystemFinder' ,
    'django.contrib.staticfiles.finders.AppDirectoriesFinder' ,

    'pipeline.finders.PipelineFinder' ,)


然后放置你的js文件和css文件进入pipeline

PIPELINE = {    
 
 
    'STYLESHEETS': {
        'colors': {
            'source_filenames': (
              'css/core.css',
              'css/colors/*.css',
              'css/layers.css'
            ),
            'output_filename': 'css/colors.css',
            'extra_context': {
                'media': 'screen,projection',
            },
        },
    },
'JAVASCRIPT' : { 'stats' : { 'source_filenames' : ( 'js/jquery.js' , 'js/d3.js' , 'js/collections/*.js' , 'js/application.js' , ), 'output_filename' : 'js/stats.js' , } } }


然后加载pipline到templates

{# pipeline>=1.4 #}
{% load pipeline %}
{% javascript 'status' %}
{% stylesheet 'colors' %}


接着用django-compress进行压缩

下载:

windows:

    1. pip install wheel==0.29.0

    2.pip install rcssmin==1.0.6 --install-option="--without-c-extensions"

    3.pip install rjsmin==1.0.12 --install-option="--without-c-extensions"

    4.pip install django_compressor==2.0

    5.然后再 pip install -U  django_compressor 升级到最新版本

接着,同上面django-pipline的操作相同,

INSTALLED_APPS = (
    # other apps
    "compressor",

)

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

)

然后配置好配置后在template加载

{% load compress %}

{% load pipeline%}

{% compress js %}

{% javascript 'status' %}

{% endcompress  %}

{% compress css%}

{% stylesheet 'colors' %}

{% endcompress %}


这时就大功告成,此时就能将合并好的js和css进行压缩

,可以大大提高加载速度,并且易于管理



猜你喜欢

转载自blog.csdn.net/qq_21570029/article/details/79493490