Django Pipeline

4.1.0 · active · verified Thu Apr 16

Django-pipeline is an active asset packaging library for Django (current version 4.1.0) that streamlines frontend development by providing CSS and JavaScript concatenation, compression, built-in JavaScript template support, and optional data-URI embedding. It integrates with Django's staticfiles system, typically releasing new versions as needed to maintain compatibility with major Django updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic configuration in `settings.py` for CSS and JavaScript asset groups. It includes setting `INSTALLED_APPS`, `STATICFILES_STORAGE`, `STATICFILES_FINDERS`, and the `PIPELINE` dictionary. In your templates, load the `pipeline` tags and use `{% stylesheet 'group_name' %}` and `{% javascript 'group_name' %}`. Remember to run `python manage.py collectstatic` after configuration for production.

import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

# settings.py snippets
INSTALLED_APPS = [
    # ... other apps
    'django.contrib.staticfiles',
    'pipeline',
]

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
    BASE_DIR / 'static',
]

STATICFILES_STORAGE = 'pipeline.storage.PipelineManifestStorage'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
)

PIPELINE = {
    'STYLESHEETS': {
        'main_css': {
            'source_filenames': (
                'css/normalize.css',
                'css/base.css',
            ),
            'output_filename': 'css/packed_css.css',
            'extra_context': {
                'media': 'screen,projection',
            },
        },
    },
    'JAVASCRIPT': {
        'main_js': {
            'source_filenames': (
                'js/jquery.js',
                'js/app.js',
            ),
            'output_filename': 'js/packed_js.js',
        }
    }
}

# Example of manually specifying a compressor binary if needed
# PIPELINE.update({
#     'YUGLIFY_BINARY': os.path.join(BASE_DIR, 'node_modules/.bin/yuglify'),
# })

# -----------------------------------------------------
# templates/base.html (or similar)
# Add at the top of the template:
# {% load pipeline %}

# In <head> for CSS:
# {% stylesheet 'main_css' %}

# Before </body> for JS:
# {% javascript 'main_js' %}

# After configuring settings.py and templates, run:
# python manage.py collectstatic

view raw JSON →