File: storages.rst

package info (click to toggle)
django-pipeline 4.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: python: 3,170; makefile: 120; javascript: 59
file content (130 lines) | stat: -rw-r--r-- 4,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
.. _ref-storages:

========
Storages
========


Using with staticfiles
======================

Pipeline is providing a storage for `staticfiles app <https://docs.djangoproject.com/en/dev/howto/static-files/>`_,
to use it configure ``STATICFILES_STORAGE`` like so ::

  STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'

And if you want versioning use ::

  STATICFILES_STORAGE = 'pipeline.storage.PipelineManifestStorage'

There is also non-packing storage available, that allows you to run ``collectstatic`` command
without packaging your assets. Useful for production when you don't want to run compressor or compilers ::

  STATICFILES_STORAGE = 'pipeline.storage.NonPackagingPipelineStorage'

Also available if you want versioning ::

  STATICFILES_STORAGE = 'pipeline.storage.NonPackagingPipelineManifestStorage'

If you use staticfiles with ``DEBUG = False`` (i.e. for integration tests
with `Selenium <http://docs.seleniumhq.org/>`_) you should install the finder
that allows staticfiles to locate your outputted assets : ::

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

If you use ``PipelineCachedStorage`` you may also like the ``CachedFileFinder``,
which allows you to use integration tests with cached file URLs.

Keep in mind that ``PipelineCachedStorage`` is only available for Django versions
before 3.1.

If you want to exclude Pipelinable content from your collected static files,
you can also use Pipeline's ``FileSystemFinder`` and ``AppDirectoriesFinder``.
These finders will also exclude `unwanted` content like READMEs, tests and
examples, which are particularly useful if you're collecting content from a
tool like Bower. ::

  STATICFILES_FINDERS = (
      'pipeline.finders.FileSystemFinder',
      'pipeline.finders.AppDirectoriesFinder',
      'pipeline.finders.CachedFileFinder',
      'pipeline.finders.PipelineFinder',
  )

GZIP compression
================

Pipeline can also creates a gzipped version of your collected static files,
so that you can avoid compressing them on the fly. ::

  STATICFILES_STORAGE = 'your.app.GZIPCachedStorage'

The storage need to inherit from ``GZIPMixin``: ::

  from django.contrib.staticfiles.storage import CachedStaticFilesStorage

  from pipeline.storage import GZIPMixin


  class GZIPCachedStorage(GZIPMixin, CachedStaticFilesStorage):
      pass


Using with other storages
=========================

You can also use your own custom storage, for example, if you want to use S3 for your assets : ::

  STATICFILES_STORAGE = 'your.app.S3PipelineManifestStorage'

Your storage only needs to inherit from ``PipelineMixin`` and ``ManifestFilesMixin`` or ``CachedFilesMixin``.

In Django 1.7+ you should use `ManifestFilesMixin <https://docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/#manifeststaticfilesstorage>`_
unless you don't have access to the local filesystem in which case you should use ``CachedFilesMixin``. ::

  from django.contrib.staticfiles.storage import CachedFilesMixin, ManifestFilesMixin

  from pipeline.storage import PipelineMixin

  from storages.backends.s3boto import S3BotoStorage

  class S3PipelineManifestStorage(PipelineMixin, ManifestFilesMixin, S3BotoStorage):
      pass

  class S3PipelineCachedStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage):
      pass


Using Pipeline with Bower
=========================

`Bower <http://bower.io/>`_ is a `package manager for the web` that allows
you to easily include frontend components with named versions. Integrating
Bower with Pipeline is straightforward.

Add your Bower directory to your ``STATICFILES_DIRS`` : ::

  STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), '..', 'bower_components'),
  )

Then process the relevant content through Pipeline : ::

  PIPELINE['JAVASCRIPT'] = {
    'components': {
      'source_filenames': (
        'jquery/jquery.js',
        # you can choose to be specific to reduce your payload
        'jquery-ui/ui/*.js',
      ),
      'output_filename': 'js/components.js',
    },
  }

``pipeline.finders.FileSystemFinder`` will help you by excluding much of the
extra content that Bower includes with its components, such as READMEs, tests
and examples, while still including images, fonts, CSS fragments etc.