Usage¶
Describes how to use Pipeline when it is installed and configured.
Form Media¶
Django forms and widgets can specify individual CSS or JavaScript files to
include on a page by defining a Form.Media
class with css
and js
attributes.
Pipeline builds upon this by allowing packages to be listed in
css_packages
and js_packages
. This is equivalent to manually including
these packages in a page’s template using the template tags.
To use these, just have your form or widget’s Media
class inherit from
pipeline.forms.PipelineFormMedia
and define css_packages
and
js_packages
. You can also continue to reference individual CSS/JavaScript
files using the original css
/js
attributes, if needed.
Note that unlike the template tags, you cannot customize the HTML for
referencing these files. The pipeline/css.html
and pipeline/js.html
files will not be used. Django takes care of generating the HTML for form and
widget media.
Example¶
from django import forms
from pipeline.forms import PipelineFormMedia
class MyForm(forms.Form):
...
class Media(PipelineFormMedia):
css_packages = {
'all': ('my-styles',)
}
js_packages = ('my-scripts',)
js = ('https://cdn.example.com/some-script.js',)
Collect static¶
Pipeline integrates with staticfiles, you just need to setup STATICFILES_STORAGE
to
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
Then when you run collectstatic
command, your CSS and your javascripts will be compressed at the same time
$ python manage.py collectstatic
Cache-busting¶
Pipeline 1.2+ no longer provides its own cache-busting URL support (using e.g. the PIPELINE_VERSIONING
setting) but uses
Django’s built-in staticfiles support for this. To set up cache-busting in conjunction with collectstatic
as above, use
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
This will handle cache-busting just as staticfiles
’s built-in CachedStaticFilesStorage
does.
Middleware¶
To enable HTML compression add pipeline.middleware.MinifyHTMLMiddleware
,
to your MIDDLEWARE_CLASSES
settings.
Ensure that it comes after any middleware which modifies your HTML, like GZipMiddleware
MIDDLEWARE_CLASSES = (
'django.middleware.gzip.GZipMiddleware',
'pipeline.middleware.MinifyHTMLMiddleware',
)
Cache manifest¶
Pipeline provide a way to add your javascripts and stylesheets files to a cache-manifest via Manifesto.
To do so, you just need to add manifesto app to your INSTALLED_APPS
.
Jinja¶
Pipeline also includes Jinja2 support and is used almost identically to the Django Template tags implementation.
You just need to pass pipeline.jinja2.PipelineExtension
to your Jinja2 environment:
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'environment': 'myproject.jinja2.environment',
'extensions': ['pipeline.jinja2.PipelineExtension']
}
}
Templates¶
Unlike the Django template tag implementation the Jinja2 implementation uses different templates, so if you wish to override them please override pipeline/css.jinja and pipeline/js.jinja.