staticfiles.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ===================
  2. The staticfiles app
  3. ===================
  4. .. module:: django.contrib.staticfiles
  5. :synopsis: An app for handling static files.
  6. .. versionadded:: 1.3
  7. ``django.contrib.staticfiles`` collects media from each of your applications
  8. (and any other places you specify) into a single location that can easily be
  9. served in production.
  10. .. seealso::
  11. For an introduction to the static files app and some usage examples, see
  12. :doc:`/howto/static-files`.
  13. .. _staticfiles-settings:
  14. Settings
  15. ========
  16. .. highlight:: python
  17. The following settings control the behavior of the static files app. Only
  18. :setting:`STATICFILES_ROOT` is required, but you'll probably also need to
  19. configure :setting:`STATICFILES_URL` as well.
  20. .. setting:: STATICFILES_ROOT
  21. STATICFILES_ROOT
  22. ----------------
  23. Default: ``''`` (Empty string)
  24. The absolute path to the directory that holds static files::
  25. STATICFILES_ROOT = "/home/example.com/static/"
  26. This is a **required setting** unless you've overridden
  27. :setting:`STATICFILES_STORAGE` and are using a custom storage backend.
  28. .. setting:: STATICFILES_URL
  29. STATICFILES_URL
  30. ---------------
  31. Default: ``'/static/'``
  32. The URL that handles the files served from :setting:`STATICFILES_ROOT`, e.g.::
  33. STATICFILES_URL = '/site_media/static/'
  34. ... or perhaps::
  35. STATICFILES_URL = 'http://media.exmaple.com/'
  36. This should **always** have a trailing slash.
  37. .. setting:: STATICFILES_DIRS
  38. STATICFILES_DIRS
  39. ----------------
  40. Default: ``[]``
  41. This setting defines the additional locations the staticfiles app will traverse
  42. if the :class:`FileSystemFinder` finder is enabled, e.g. if you use the
  43. :djadmin:`collectstatic` or :djadmin:`findstatic` management command or use the
  44. static file serving view.
  45. It should be defined as a sequence of ``(prefix, path)`` tuples, e.g.::
  46. STATICFILES_DIRS = (
  47. ('', '/home/special.polls.com/polls/media'),
  48. ('', '/home/polls.com/polls/media'),
  49. ('common', '/opt/webfiles/common'),
  50. )
  51. .. setting:: STATICFILES_STORAGE
  52. STATICFILES_STORAGE
  53. -------------------
  54. Default: ``'django.contrib.staticfiles.storage.StaticFilesStorage'``
  55. The file storage engine to use when collecting static files with the
  56. :djadmin:`collectstatic` management command.
  57. For an example, see :ref:`staticfiles-from-cdn`.
  58. .. setting:: STATICFILES_FINDERS
  59. STATICFILES_FINDERS
  60. -------------------
  61. Default::
  62. ("django.contrib.staticfiles.finders.FileSystemFinder",
  63. "django.contrib.staticfiles.finders.AppDirectoriesFinder")
  64. The list of finder backends that know how to find static files in
  65. various locations.
  66. The default will find files stored in the :setting:`STATICFILES_DIRS` setting
  67. (using :class:`django.contrib.staticfiles.finders.FileSystemFinder`) and in a
  68. ``static`` subdirectory of each app (using
  69. :class:`django.contrib.staticfiles.finders.AppDirectoriesFinder`)
  70. One finder is disabled by default:
  71. :class:`django.contrib.staticfiles.finders.DefaultStorageFinder`. If added to
  72. your :setting:`STATICFILES_FINDERS` setting, it will look for static files in
  73. the default file storage as defined by the :setting:`DEFAULT_FILE_STORAGE`
  74. setting.
  75. .. note::
  76. When using the :class:AppDirectoriesFinder` finder, make sure your apps can
  77. be found by Django's app loading mechanism. Simply include a ``models``
  78. module (an empty ``models.py`` file suffices) and add the app to the
  79. :setting:`INSTALLED_APPS` setting of your site.
  80. Static file finders are currently considered a private interface, and this
  81. interface is thus undocumented.
  82. Management Commands
  83. ===================
  84. .. highlight:: console
  85. ``django.contrib.staticfiles`` exposes two management commands.
  86. collectstatic
  87. -------------
  88. .. django-admin:: collectstatic
  89. Collects the static files into :setting:`STATICFILES_ROOT`.
  90. Duplicate file names are resolved in a similar way to how template resolution
  91. works: files from apps later in :setting:`INSTALLED_APPS` overwrite those from
  92. earlier apps, and files from storage directories later in
  93. :setting:`STATICFILES_DIRS` overwrite those from earlier. If you're confused,
  94. the :djadmin:`findstatic` command can help show you where
  95. Files are searched by using the :ref:`enabled finders
  96. <staticfiles-finders>`. The default is to look in all locations defined in
  97. :ref:`staticfiles-dirs` and in the ``media`` directory of apps specified by the
  98. :setting:`INSTALLED_APPS` setting.
  99. Some commonly used options are:
  100. ``--noinput``
  101. Do NOT prompt the user for input of any kind.
  102. ``-i PATTERN`` or ``--ignore=PATTERN``
  103. Ignore files or directories matching this glob-style pattern. Use multiple
  104. times to ignore more.
  105. ``-n`` or ``--dry-run``
  106. Do everything except modify the filesystem.
  107. ``-l`` or ``--link``
  108. Create a symbolic link to each file instead of copying.
  109. ``--no-default-ignore``
  110. Don't ignore the common private glob-style patterns ``'CVS'``, ``'.*'``
  111. and ``'*~'``.
  112. For a full list of options, refer to the commands own help by running::
  113. $ python manage.py collectstatic --help
  114. findstatic
  115. ----------
  116. .. django-admin:: findstatic
  117. Searches for one or more relative paths with the enabled finders.
  118. For example::
  119. $ python manage.py findstatic css/base.css admin/js/core.js
  120. /home/special.polls.com/core/media/css/base.css
  121. /home/polls.com/core/media/css/base.css
  122. /home/polls.com/src/django/contrib/admin/media/js/core.js
  123. By default, all matching locations are found. To only return the first match
  124. for each relative path, use the ``--first`` option::
  125. $ python manage.py findstatic css/base.css --first
  126. /home/special.polls.com/core/media/css/base.css
  127. This is a debugging aid; it'll show you exactly which static file will be
  128. collected for a given path.
  129. Other Helpers
  130. =============
  131. The ``media`` context processor
  132. -------------------------------
  133. .. function:: django.contrib.staticfiles.context_processors.staticfiles
  134. This context processor adds the :setting:`STATICFILES_URL` into each template
  135. context as the variable ``{{ STATICFILES_URL }}``. To use it, make sure that
  136. ``'django.contrib.staticfiles.context_processors.staticfiles'`` appears
  137. somewhere in your :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting.
  138. Remember, only templates rendered with :class:`~django.template.RequestContext`
  139. will have acces to the data provided by this (and any) context processor.
  140. .. templatetag:: get_staticfiles_prefix
  141. The ``get_staticfiles_prefix`` templatetag
  142. ==========================================
  143. .. highlight:: html+django
  144. If you're not using :class:`~django.template.RequestContext`, or if you need
  145. more control over exactly where and how :setting:`STATICFILES_URL` is injected
  146. into the template, you can use the :ttag:`get_staticfiles_prefix` template tag
  147. instead::
  148. {% load staticfiles %}
  149. <img src="{% get_staticfiles_prefix %}images/hi.jpg" />
  150. There's also a second form you can use to avoid extra processing if you need the
  151. value multiple times::
  152. {% load staticfiles %}
  153. {% get_staticfiles_prefix as STATIC_PREFIX %}
  154. <img src="{{ STATIC_PREFIX }}images/hi.jpg" />
  155. <img src="{{ STATIC_PREFIX }}images/hi2.jpg" />
  156. .. _staticfiles-development-view:
  157. Static file development view
  158. ----------------------------
  159. .. highlight:: python
  160. .. function:: django.contrib.staticfiles.views.serve(request, path)
  161. This view function serves static media in in development.
  162. .. warning::
  163. This view will only work if :setting:`DEBUG` is ``True``.
  164. That's because this view is **grossly inefficient** and probably
  165. **insecure**. This is only intended for local development, and should
  166. **never be used in production**.
  167. To use the view, add the following snippet to the end of your primary URL
  168. configuration::
  169. from django.conf import settings
  170. if settings.DEBUG:
  171. urlpatterns = patterns('django.contrib.staticfiles.views',
  172. url(r'^static/(?P<path>.*)$', 'serve'),
  173. )
  174. Note, the begin of the pattern (``r'^static/'``) should be your
  175. :setting:`STATICFILES_URL` setting.
  176. Since this is a bit finicky, there's also a helper function that'll do this for you:
  177. .. function:: django.contrib.staticfiles.urls.staticfiles_urlpatterns()
  178. This will return the proper URL pattern for serving static files to your
  179. already defined pattern list. Use it like this::
  180. from django.contrib.staticfiles.urls import staticfiles_urlpatterns
  181. # ... the rest of your URLconf here ...
  182. urlpatterns += staticfiles_urlpatterns()