【问题标题】:Dynamically compressing static files with django-pipeline使用 django-pipeline 动态压缩静态文件
【发布时间】:2014-06-19 06:30:34
【问题描述】:

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

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

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

【问题讨论】:

    标签: python django django-pipeline


    【解决方案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',
        }
    }
    

    【讨论】:

    • 我知道 glob 语法,但是我不能将所有 CSS 文件包含在一个文件中,因为它会很混乱(即逐节覆盖属性)。更像是,收集文件并压缩它们,我不需要将它们分组,它们已经分组了。
    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2015-08-12
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多