使用django-pipeline动态压缩静态文件

时间:2014-06-19 06:30:34

标签: python django django-pipeline

我开始使用django-pipeline。如果我理解正确,我需要用我的CSS / JS文件指定目录来压缩它们。但是,这是一项繁琐的任务,因为我的项目很大,并且在这里和那里都有静态文件(不仅在/ static /目录下)。

我看到它有collectstatic集成,但它不是我想的:它只是在收集静态文件后运行压缩器,并且只会压缩你在设置中手动指定的文件而不是所有静态文件。

有什么方法可以告诉django-pipeline只压缩我的每个静态文件?

1 个答案:

答案 0 :(得分:1)

您可以使用glob语法选择多个文件。就像这样,

您不想使用collectstatic吗?

然后用这种方式,

from django.contrib.staticfiles import finders

all_js = ["{0}/{1}".format(st,"*.js") for st in finders.find("", all=True)]
all_css = ["{0}/{1}".format(st,"*.css") for st in finders.find("", all=True)]

PIPELINE_CSS = {
    'colors': {
        'source_filenames': tuple(all_css),
        'output_filename': 'css/colors.css',
        'extra_context': {
            'media': 'screen,projection',
        },
    },
}

PIPELINE_JS = {
    'stats': {
        'source_filenames': tuple(all_js),
        'output_filename': 'js/stats.js',
    }
}