summaryrefslogtreecommitdiff
path: root/docs/ref/contrib/staticfiles.txt
blob: 2c300a1d03fe34b53acd6eb0742f8b4cf93dc0e1 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
=======================
The ``staticfiles`` app
=======================

.. module:: django.contrib.staticfiles
   :synopsis: An app for handling static files.

``django.contrib.staticfiles`` collects static files from each of your
applications (and any other places you specify) into a single location that
can easily be served in production.

.. seealso::

    For an introduction to the static files app and some usage examples, see
    :doc:`/howto/static-files/index`. For guidelines on deploying static files,
    see :doc:`/howto/static-files/deployment`.

.. _staticfiles-settings:

Settings
========

See :ref:`staticfiles settings <settings-staticfiles>` for details on the
following settings:

* :setting:`STATIC_ROOT`
* :setting:`STATIC_URL`
* :setting:`STATICFILES_DIRS`
* :setting:`STATICFILES_STORAGE`
* :setting:`STATICFILES_FINDERS`

Management Commands
===================

``django.contrib.staticfiles`` exposes three management commands.

``collectstatic``
-----------------

.. django-admin:: collectstatic

Collects the static files into :setting:`STATIC_ROOT`.

Duplicate file names are by default resolved in a similar way to how template
resolution works: the file that is first found in one of the specified
locations will be used. If you're confused, the :djadmin:`findstatic` command
can help show you which files are found.

On subsequent ``collectstatic`` runs (if ``STATIC_ROOT`` isn't empty), files
are copied only if they have a modified timestamp greater than the timestamp of
the file in ``STATIC_ROOT``. Therefore if you remove an application from
:setting:`INSTALLED_APPS`, it's a good idea to use the :option:`collectstatic
--clear` option in order to remove stale static files.

Files are searched by using the :setting:`enabled finders
<STATICFILES_FINDERS>`. The default is to look in all locations defined in
:setting:`STATICFILES_DIRS` and in the ``'static'`` directory of apps
specified by the :setting:`INSTALLED_APPS` setting.

The :djadmin:`collectstatic` management command calls the
:meth:`~django.contrib.staticfiles.storage.StaticFilesStorage.post_process`
method of the :setting:`STATICFILES_STORAGE` after each run and passes
a list of paths that have been found by the management command. It also
receives all command line options of :djadmin:`collectstatic`. This is used
by the :class:`~django.contrib.staticfiles.storage.CachedStaticFilesStorage`
by default.

By default, collected files receive permissions from
:setting:`FILE_UPLOAD_PERMISSIONS` and collected directories receive permissions
from :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`. If you would like different
permissions for these files and/or directories, you can subclass either of the
:ref:`static files storage classes <staticfiles-storages>` and specify the
``file_permissions_mode`` and/or ``directory_permissions_mode`` parameters,
respectively. For example::

    from django.contrib.staticfiles import storage

    class MyStaticFilesStorage(storage.StaticFilesStorage):
        def __init__(self, *args, **kwargs):
            kwargs['file_permissions_mode'] = 0o640
            kwargs['directory_permissions_mode'] = 0o760
            super().__init__(*args, **kwargs)

Then set the :setting:`STATICFILES_STORAGE` setting to
``'path.to.MyStaticFilesStorage'``.

.. highlight:: console

Some commonly used options are:

.. django-admin-option:: --noinput, --no-input

    Do NOT prompt the user for input of any kind.

.. django-admin-option:: --ignore PATTERN, -i PATTERN

    Ignore files or directories matching this glob-style pattern. Use multiple
    times to ignore more.

.. django-admin-option:: --dry-run, -n

    Do everything except modify the filesystem.

.. django-admin-option:: --clear, -c

    Clear the existing files before trying to copy or link the original file.

.. django-admin-option:: --link, -l

    Create a symbolic link to each file instead of copying.

.. django-admin-option:: --no-post-process

    Don't call the
    :meth:`~django.contrib.staticfiles.storage.StaticFilesStorage.post_process`
    method of the configured :setting:`STATICFILES_STORAGE` storage backend.

.. django-admin-option:: --no-default-ignore

    Don't ignore the common private glob-style patterns ``'CVS'``, ``'.*'``
    and ``'*~'``.

For a full list of options, refer to the commands own help by running::

   $ python manage.py collectstatic --help

.. _customize-staticfiles-ignore-patterns:

Customizing the ignored pattern list
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The default ignored pattern list, ``['CVS', '.*', '*~']``, can be customized in
a more persistent way than providing the ``--ignore`` command option at each
``collectstatic`` invocation. Provide a custom :class:`~django.apps.AppConfig`
class, override the ``ignore_patterns`` attribute of this class and replace
``'django.contrib.staticfiles'`` with that class path in your
:setting:`INSTALLED_APPS` setting:

.. code-block:: python

    from django.contrib.staticfiles.apps import StaticFilesConfig

    class MyStaticFilesConfig(StaticFilesConfig):
        ignore_patterns = [...]  # your custom ignore list

``findstatic``
--------------

.. django-admin:: findstatic staticfile [staticfile ...]

Searches for one or more relative paths with the enabled finders.

For example::

   $ python manage.py findstatic css/base.css admin/js/core.js
   Found 'css/base.css' here:
     /home/special.polls.com/core/static/css/base.css
     /home/polls.com/core/static/css/base.css
   Found 'admin/js/core.js' here:
     /home/polls.com/src/django/contrib/admin/media/js/core.js

.. django-admin-option:: findstatic --first

By default, all matching locations are found. To only return the first match
for each relative path, use the ``--first`` option::

   $ python manage.py findstatic css/base.css --first
   Found 'css/base.css' here:
     /home/special.polls.com/core/static/css/base.css

This is a debugging aid; it'll show you exactly which static file will be
collected for a given path.

By setting the ``--verbosity`` flag to 0, you can suppress the extra output and
just get the path names::

   $ python manage.py findstatic css/base.css --verbosity 0
   /home/special.polls.com/core/static/css/base.css
   /home/polls.com/core/static/css/base.css

On the other hand, by setting the ``--verbosity`` flag to 2, you can get all
the directories which were searched::

   $ python manage.py findstatic css/base.css --verbosity 2
   Found 'css/base.css' here:
     /home/special.polls.com/core/static/css/base.css
     /home/polls.com/core/static/css/base.css
   Looking in the following locations:
     /home/special.polls.com/core/static
     /home/polls.com/core/static
     /some/other/path/static

.. _staticfiles-runserver:

``runserver``
-------------

.. django-admin:: runserver [addrport]

Overrides the core :djadmin:`runserver` command if the ``staticfiles`` app
is :setting:`installed<INSTALLED_APPS>` and adds automatic serving of static
files. File serving doesn't run through :setting:`MIDDLEWARE`.

The command adds these options:

.. django-admin-option:: --nostatic

Use the ``--nostatic`` option to disable serving of static files with the
:doc:`staticfiles </ref/contrib/staticfiles>` app entirely. This option is
only available if the :doc:`staticfiles </ref/contrib/staticfiles>` app is
in your project's :setting:`INSTALLED_APPS` setting.

Example usage::

    django-admin runserver --nostatic

.. django-admin-option:: --insecure

Use the ``--insecure`` option to force serving of static files with the
:doc:`staticfiles </ref/contrib/staticfiles>` app even if the :setting:`DEBUG`
setting is ``False``. By using this you acknowledge the fact that it's
**grossly inefficient** and probably **insecure**. This is only intended for
local development, should **never be used in production** and is only
available if the :doc:`staticfiles </ref/contrib/staticfiles>` app is
in your project's :setting:`INSTALLED_APPS` setting.

``--insecure`` doesn't work with
:class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` or
:class:`~django.contrib.staticfiles.storage.CachedStaticFilesStorage`.

Example usage::

    django-admin runserver --insecure

.. _staticfiles-storages:

Storages
========

``StaticFilesStorage``
----------------------

.. class:: storage.StaticFilesStorage

A subclass of the :class:`~django.core.files.storage.FileSystemStorage`
storage backend that uses the :setting:`STATIC_ROOT` setting as the base
file system location and the :setting:`STATIC_URL` setting respectively
as the base URL.

.. method:: storage.StaticFilesStorage.post_process(paths, **options)

This method is called by the :djadmin:`collectstatic` management command
after each run and gets passed the local storages and paths of found
files as a dictionary, as well as the command line options.

The :class:`~django.contrib.staticfiles.storage.CachedStaticFilesStorage`
uses this behind the scenes to replace the paths with their hashed
counterparts and update the cache appropriately.

``ManifestStaticFilesStorage``
------------------------------

.. class:: storage.ManifestStaticFilesStorage

A subclass of the :class:`~django.contrib.staticfiles.storage.StaticFilesStorage`
storage backend which stores the file names it handles by appending the MD5
hash of the file's content to the filename. For example, the file
``css/styles.css`` would also be saved as ``css/styles.55e7cbb9ba48.css``.

The purpose of this storage is to keep serving the old files in case some
pages still refer to those files, e.g. because they are cached by you or
a 3rd party proxy server. Additionally, it's very helpful if you want to
apply `far future Expires headers`_ to the deployed files to speed up the
load time for subsequent page visits.

The storage backend automatically replaces the paths found in the saved
files matching other saved files with the path of the cached copy (using
the :meth:`~django.contrib.staticfiles.storage.StaticFilesStorage.post_process`
method). The regular expressions used to find those paths
(``django.contrib.staticfiles.storage.HashedFilesMixin.patterns``)
by default covers the `@import`_ rule and `url()`_ statement of `Cascading
Style Sheets`_. For example, the ``'css/styles.css'`` file with the
content

.. code-block:: css+django

    @import url("../admin/css/base.css");

would be replaced by calling the :meth:`~django.core.files.storage.Storage.url`
method of the ``ManifestStaticFilesStorage`` storage backend, ultimately
saving a ``'css/styles.55e7cbb9ba48.css'`` file with the following
content:

.. code-block:: css+django

    @import url("../admin/css/base.27e20196a850.css");

.. attribute:: storage.ManifestStaticFilesStorage.max_post_process_passes

Since static files might reference other static files that need to have their
paths replaced, multiple passes of replacing paths may be needed until the file
hashes converge. To prevent an infinite loop due to hashes not converging (for
example, if ``'foo.css'`` references ``'bar.css'`` which references
``'foo.css'``) there is a maximum number of passes before post-processing is
abandoned. In cases with a large number of references, a higher number of
passes might be needed. Increase the maximum number of passes by subclassing
``ManifestStaticFilesStorage`` and setting the ``max_post_process_passes``
attribute. It defaults to 5.

To enable the ``ManifestStaticFilesStorage`` you have to make sure the
following requirements are met:

* the :setting:`STATICFILES_STORAGE` setting is set to
  ``'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'``
* the :setting:`DEBUG` setting is set to ``False``
* you've collected all your static files by using the
  :djadmin:`collectstatic` management command

Since creating the MD5 hash can be a performance burden to your website
during runtime, ``staticfiles`` will automatically store the mapping with
hashed names for all processed files in a file called ``staticfiles.json``.
This happens once when you run the :djadmin:`collectstatic` management
command.

.. attribute:: storage.ManifestStaticFilesStorage.manifest_strict

If a file isn't found in the ``staticfiles.json`` manifest at runtime, a
``ValueError`` is raised. This behavior can be disabled by subclassing
``ManifestStaticFilesStorage`` and setting the ``manifest_strict`` attribute to
``False`` -- nonexistent paths will remain unchanged.

Due to the requirement of running :djadmin:`collectstatic`, this storage
typically shouldn't be used when running tests as ``collectstatic`` isn't run
as part of the normal test setup. During testing, ensure that the
:setting:`STATICFILES_STORAGE` setting is set to something else like
``'django.contrib.staticfiles.storage.StaticFilesStorage'`` (the default).

.. method:: storage.ManifestStaticFilesStorage.file_hash(name, content=None)

The method that is used when creating the hashed name of a file.
Needs to return a hash for the given file name and content.
By default it calculates a MD5 hash from the content's chunks as
mentioned above. Feel free to override this method to use your own
hashing algorithm.

.. _`far future Expires headers`: https://developer.yahoo.com/performance/rules.html#expires
.. _`@import`: https://www.w3.org/TR/CSS2/cascade.html#at-import
.. _`url()`: https://www.w3.org/TR/CSS2/syndata.html#uri
.. _`Cascading Style Sheets`: https://www.w3.org/Style/CSS/

``CachedStaticFilesStorage``
----------------------------

.. class:: storage.CachedStaticFilesStorage

``CachedStaticFilesStorage`` is a similar class like the
:class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` class
but uses Django's :doc:`caching framework</topics/cache>` for storing the
hashed names of processed files instead of a static manifest file called
``staticfiles.json``. This is mostly useful for situations in which you don't
have access to the file system.

If you want to override certain options of the cache backend the storage uses,
simply specify a custom entry in the :setting:`CACHES` setting named
``'staticfiles'``. It falls back to using the ``'default'`` cache backend.

.. warning::

    ``CachedStaticFilesStorage`` isn't recommended --  in almost all cases
    ``ManifestStaticFilesStorage`` is a better choice. There are several
    performance penalties when using ``CachedStaticFilesStorage`` since a cache
    miss requires hashing files at runtime. Remote file storage require several
    round-trips to hash a file on a cache miss, as several file accesses are
    required to ensure that the file hash is correct in the case of nested file
    paths.

Finders Module
==============

``staticfiles`` finders has a ``searched_locations`` attribute which is a list
of directory paths in which the finders searched. Example usage::

    from django.contrib.staticfiles import finders

    result = finders.find('css/base.css')
    searched_locations = finders.searched_locations

Other Helpers
=============

There are a few other helpers outside of the
:mod:`staticfiles <django.contrib.staticfiles>` app to work with static
files:

- The :func:`django.template.context_processors.static` context processor
  which adds :setting:`STATIC_URL` to every template context rendered
  with :class:`~django.template.RequestContext` contexts.

- The builtin template tag :ttag:`static` which takes a path and urljoins it
  with the static prefix :setting:`STATIC_URL`. If
  ``django.contrib.staticfiles`` is installed, the tag uses the ``url()``
  method of the :setting:`STATICFILES_STORAGE` instead.

- The builtin template tag :ttag:`get_static_prefix` which populates a
  template variable with the static prefix :setting:`STATIC_URL` to be
  used as a variable or directly.

- The similar template tag :ttag:`get_media_prefix` which works like
  :ttag:`get_static_prefix` but uses :setting:`MEDIA_URL`.

.. _staticfiles-development-view:

Static file development view
----------------------------

.. currentmodule:: django.contrib.staticfiles

The static files tools are mostly designed to help with getting static files
successfully deployed into production. This usually means a separate,
dedicated static file server, which is a lot of overhead to mess with when
developing locally. Thus, the ``staticfiles`` app ships with a
**quick and dirty helper view** that you can use to serve files locally in
development.

.. highlight:: python

.. function:: views.serve(request, path)

This view function serves static files in development.

.. warning::

    This view will only work if :setting:`DEBUG` is ``True``.

    That's because this view is **grossly inefficient** and probably
    **insecure**. This is only intended for local development, and should
    **never be used in production**.

.. note::

    To guess the served files' content types, this view relies on the
    :py:mod:`mimetypes` module from the Python standard library, which itself
    relies on the underlying platform's map files. If you find that this view
    doesn't return proper content types for certain files, it is most likely
    that the platform's map files need to be updated. This can be achieved, for
    example, by installing or updating the ``mailcap`` package on a Red Hat
    distribution, or ``mime-support`` on a Debian distribution.

This view is automatically enabled by :djadmin:`runserver` (with a
:setting:`DEBUG` setting set to ``True``). To use the view with a different
local development server, add the following snippet to the end of your
primary URL configuration::

   from django.conf import settings
   from django.contrib.staticfiles import views
   from django.urls import re_path

   if settings.DEBUG:
       urlpatterns += [
           re_path(r'^static/(?P<path>.*)$', views.serve),
       ]

Note, the beginning of the pattern (``r'^static/'``) should be your
:setting:`STATIC_URL` setting.

Since this is a bit finicky, there's also a helper function that'll do this for
you:

.. function:: urls.staticfiles_urlpatterns()

This will return the proper URL pattern for serving static files to your
already defined pattern list. Use it like this::

   from django.contrib.staticfiles.urls import staticfiles_urlpatterns

   # ... the rest of your URLconf here ...

   urlpatterns += staticfiles_urlpatterns()

This will inspect your :setting:`STATIC_URL` setting and wire up the view
to serve static files accordingly. Don't forget to set the
:setting:`STATICFILES_DIRS` setting appropriately to let
``django.contrib.staticfiles`` know where to look for files in addition to
files in app directories.

.. warning::

    This helper function will only work if :setting:`DEBUG` is ``True``
    and your :setting:`STATIC_URL` setting is neither empty nor a full
    URL such as ``http://static.example.com/``.

    That's because this view is **grossly inefficient** and probably
    **insecure**. This is only intended for local development, and should
    **never be used in production**.

Specialized test case to support 'live testing'
-----------------------------------------------

.. class:: testing.StaticLiveServerTestCase

This unittest TestCase subclass extends :class:`django.test.LiveServerTestCase`.

Just like its parent, you can use it to write tests that involve running the
code under test and consuming it with testing tools through HTTP (e.g. Selenium,
PhantomJS, etc.), because of which it's needed that the static assets are also
published.

But given the fact that it makes use of the
:func:`django.contrib.staticfiles.views.serve` view described above, it can
transparently overlay at test execution-time the assets provided by the
``staticfiles`` finders. This means you don't need to run
:djadmin:`collectstatic` before or as a part of your tests setup.