staticfiles.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 static files from each of your
  8. applications (and any other places you specify) into a single location that
  9. can easily be 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. .. note::
  18. The following settings control the behavior of the staticfiles app.
  19. .. setting:: STATICFILES_DIRS
  20. STATICFILES_DIRS
  21. ----------------
  22. Default: ``[]``
  23. This setting defines the additional locations the staticfiles app will traverse
  24. if the :class:`FileSystemFinder` finder is enabled, e.g. if you use the
  25. :djadmin:`collectstatic` or :djadmin:`findstatic` management command or use the
  26. static file serving view.
  27. This should be set to a list or tuple of strings that contain full paths to
  28. your additional files directory(ies) e.g.::
  29. STATICFILES_DIRS = (
  30. "/home/special.polls.com/polls/static",
  31. "/home/polls.com/polls/static",
  32. "/opt/webfiles/common",
  33. )
  34. Prefixes (optional)
  35. """""""""""""""""""
  36. In case you want to refer to files in one of the locations with an additional
  37. namespace, you can **optionally** provide a prefix as ``(prefix, path)``
  38. tuples, e.g.::
  39. STATICFILES_DIRS = (
  40. # ...
  41. ("downloads", "/opt/webfiles/stats"),
  42. )
  43. Example:
  44. Assuming you have :setting:`STATIC_URL` set ``'/static/'``, the
  45. :djadmin:`collectstatic` management command would collect the "stats" files
  46. in a ``'downloads'`` subdirectory of :setting:`STATIC_ROOT`.
  47. This would allow you to refer to the local file
  48. ``'/opt/webfiles/stats/polls_20101022.tar.gz'`` with
  49. ``'/static/downloads/polls_20101022.tar.gz'`` in your templates, e.g.::
  50. <a href="{{ STATIC_URL }}downloads/polls_20101022.tar.gz">
  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
  77. can be found by staticfiles. Simply add the app to the
  78. :setting:`INSTALLED_APPS` setting of your site.
  79. Static file finders are currently considered a private interface, and this
  80. interface is thus undocumented.
  81. Management Commands
  82. ===================
  83. .. highlight:: console
  84. ``django.contrib.staticfiles`` exposes three management commands.
  85. collectstatic
  86. -------------
  87. .. django-admin:: collectstatic
  88. Collects the static files into :setting:`STATIC_ROOT`.
  89. Duplicate file names are by default resolved in a similar way to how template
  90. resolution works: the file that is first found in one of the specified
  91. locations will be used. If you're confused, the :djadmin:`findstatic` command
  92. can help show you which files are found.
  93. Files are searched by using the :setting:`enabled finders
  94. <STATICFILES_FINDERS>`. The default is to look in all locations defined in
  95. :setting:`STATICFILES_DIRS` and in the ``'static'`` directory of apps
  96. specified by the :setting:`INSTALLED_APPS` setting.
  97. Some commonly used options are:
  98. ``--noinput``
  99. Do NOT prompt the user for input of any kind.
  100. ``-i PATTERN`` or ``--ignore=PATTERN``
  101. Ignore files or directories matching this glob-style pattern. Use multiple
  102. times to ignore more.
  103. ``-n`` or ``--dry-run``
  104. Do everything except modify the filesystem.
  105. ``-l`` or ``--link``
  106. Create a symbolic link to each file instead of copying.
  107. ``--no-default-ignore``
  108. Don't ignore the common private glob-style patterns ``'CVS'``, ``'.*'``
  109. and ``'*~'``.
  110. For a full list of options, refer to the commands own help by running::
  111. $ python manage.py collectstatic --help
  112. findstatic
  113. ----------
  114. .. django-admin:: findstatic
  115. Searches for one or more relative paths with the enabled finders.
  116. For example::
  117. $ python manage.py findstatic css/base.css admin/js/core.js
  118. /home/special.polls.com/core/static/css/base.css
  119. /home/polls.com/core/static/css/base.css
  120. /home/polls.com/src/django/contrib/admin/media/js/core.js
  121. By default, all matching locations are found. To only return the first match
  122. for each relative path, use the ``--first`` option::
  123. $ python manage.py findstatic css/base.css --first
  124. /home/special.polls.com/core/static/css/base.css
  125. This is a debugging aid; it'll show you exactly which static file will be
  126. collected for a given path.
  127. .. _staticfiles-runserver:
  128. runserver
  129. ---------
  130. .. django-admin:: runserver
  131. Overrides the core :djadmin:`runserver` command if the ``staticfiles`` app
  132. is :setting:`installed<INSTALLED_APPS>` and adds automatic serving of static
  133. files and the following new options.
  134. .. django-admin-option:: --nostatic
  135. Use the ``--nostatic`` option to disable serving of static files with the
  136. :doc:`staticfiles </ref/contrib/staticfiles>` app entirely. This option is
  137. only available if the :doc:`staticfiles </ref/contrib/staticfiles>` app is
  138. in your project's :setting:`INSTALLED_APPS` setting.
  139. Example usage::
  140. django-admin.py runserver --nostatic
  141. .. django-admin-option:: --insecure
  142. Use the ``--insecure`` option to force serving of static files with the
  143. :doc:`staticfiles </ref/contrib/staticfiles>` app even if the :setting:`DEBUG`
  144. setting is ``False``. By using this you acknowledge the fact that it's
  145. **grossly inefficient** and probably **insecure**. This is only intended for
  146. local development, should **never be used in production** and is only
  147. available if the :doc:`staticfiles </ref/contrib/staticfiles>` app is
  148. in your project's :setting:`INSTALLED_APPS` setting.
  149. Example usage::
  150. django-admin.py runserver --insecure
  151. .. currentmodule:: None
  152. Other Helpers
  153. =============
  154. The ``static`` context processor
  155. --------------------------------
  156. .. function:: django.core.context_processors.static
  157. This context processor adds the :setting:`STATIC_URL` into each template
  158. context as the variable ``{{ STATIC_URL }}``. To use it, make sure that
  159. ``'django.core.context_processors.static'`` appears somewhere in your
  160. :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting.
  161. Remember, only templates rendered with :class:`~django.template.RequestContext`
  162. will have acces to the data provided by this (and any) context processor.
  163. .. templatetag:: get_static_prefix
  164. The ``get_static_prefix`` templatetag
  165. =====================================
  166. .. highlight:: html+django
  167. If you're not using :class:`~django.template.RequestContext`, or if you need
  168. more control over exactly where and how :setting:`STATIC_URL` is injected
  169. into the template, you can use the :ttag:`get_static_prefix` template tag
  170. instead::
  171. {% load static %}
  172. <img src="{% get_static_prefix %}images/hi.jpg" />
  173. There's also a second form you can use to avoid extra processing if you need
  174. the value multiple times::
  175. {% load static %}
  176. {% get_static_prefix as STATIC_PREFIX %}
  177. <img src="{{ STATIC_PREFIX }}images/hi.jpg" />
  178. <img src="{{ STATIC_PREFIX }}images/hi2.jpg" />
  179. .. _staticfiles-development-view:
  180. Static file development view
  181. ----------------------------
  182. .. highlight:: python
  183. .. function:: django.contrib.staticfiles.views.serve(request, path)
  184. This view function serves static files in development.
  185. .. warning::
  186. This view will only work if :setting:`DEBUG` is ``True``.
  187. That's because this view is **grossly inefficient** and probably
  188. **insecure**. This is only intended for local development, and should
  189. **never be used in production**.
  190. This view is automatically enabled by :djadmin:`runserver` (with a
  191. :setting:`DEBUG` setting set to ``True``). To use the view with a different
  192. local development server, add the following snippet to the end of your
  193. primary URL configuration::
  194. from django.conf import settings
  195. if settings.DEBUG:
  196. urlpatterns += patterns('django.contrib.staticfiles.views',
  197. url(r'^static/(?P<path>.*)$', 'serve'),
  198. )
  199. Note, the begin of the pattern (``r'^static/'``) should be your
  200. :setting:`STATIC_URL` setting.
  201. Since this is a bit finicky, there's also a helper function that'll do this for you:
  202. .. function:: django.contrib.staticfiles.urls.staticfiles_urlpatterns()
  203. This will return the proper URL pattern for serving static files to your
  204. already defined pattern list. Use it like this::
  205. from django.contrib.staticfiles.urls import staticfiles_urlpatterns
  206. # ... the rest of your URLconf here ...
  207. urlpatterns += staticfiles_urlpatterns()
  208. .. warning::
  209. This helper function will only work if :setting:`DEBUG` is ``True``
  210. and your :setting:`STATIC_URL` setting is neither empty nor a full
  211. URL such as ``http://static.example.com/``.