1. =========================================================
    
  2. How to manage static files (e.g. images, JavaScript, CSS)
    
  3. =========================================================
    
  4. 
    
  5. Websites generally need to serve additional files such as images, JavaScript,
    
  6. or CSS. In Django, we refer to these files as "static files".  Django provides
    
  7. :mod:`django.contrib.staticfiles` to help you manage them.
    
  8. 
    
  9. This page describes how you can serve these static files.
    
  10. 
    
  11. Configuring static files
    
  12. ========================
    
  13. 
    
  14. #. Make sure that ``django.contrib.staticfiles`` is included in your
    
  15.    :setting:`INSTALLED_APPS`.
    
  16. 
    
  17. #. In your settings file, define :setting:`STATIC_URL`, for example::
    
  18. 
    
  19.       STATIC_URL = 'static/'
    
  20. 
    
  21. #. In your templates, use the :ttag:`static` template tag to build the URL for
    
  22.    the given relative path using the configured :setting:`STATICFILES_STORAGE`.
    
  23. 
    
  24.    .. _staticfiles-in-templates:
    
  25. 
    
  26.    .. code-block:: html+django
    
  27. 
    
  28.         {% load static %}
    
  29.         <img src="{% static 'my_app/example.jpg' %}" alt="My image">
    
  30. 
    
  31. #. Store your static files in a folder called ``static`` in your app. For
    
  32.    example ``my_app/static/my_app/example.jpg``.
    
  33. 
    
  34. .. admonition:: Serving the files
    
  35. 
    
  36.     In addition to these configuration steps, you'll also need to actually
    
  37.     serve the static files.
    
  38. 
    
  39.     During development, if you use :mod:`django.contrib.staticfiles`, this will
    
  40.     be done automatically by :djadmin:`runserver` when :setting:`DEBUG` is set
    
  41.     to ``True`` (see :func:`django.contrib.staticfiles.views.serve`).
    
  42. 
    
  43.     This method is **grossly inefficient** and probably **insecure**,
    
  44.     so it is **unsuitable for production**.
    
  45. 
    
  46.     See :doc:`/howto/static-files/deployment` for proper strategies to serve
    
  47.     static files in production environments.
    
  48. 
    
  49. Your project will probably also have static assets that aren't tied to a
    
  50. particular app. In addition to using a ``static/`` directory inside your apps,
    
  51. you can define a list of directories (:setting:`STATICFILES_DIRS`) in your
    
  52. settings file where Django will also look for static files. For example::
    
  53. 
    
  54.     STATICFILES_DIRS = [
    
  55.         BASE_DIR / "static",
    
  56.         '/var/www/static/',
    
  57.     ]
    
  58. 
    
  59. See the documentation for the :setting:`STATICFILES_FINDERS` setting for
    
  60. details on how ``staticfiles`` finds your files.
    
  61. 
    
  62. .. admonition:: Static file namespacing
    
  63. 
    
  64.     Now we *might* be able to get away with putting our static files directly
    
  65.     in ``my_app/static/`` (rather than creating another ``my_app``
    
  66.     subdirectory), but it would actually be a bad idea. Django will use the
    
  67.     first static file it finds whose name matches, and if you had a static file
    
  68.     with the same name in a *different* application, Django would be unable to
    
  69.     distinguish between them. We need to be able to point Django at the right
    
  70.     one, and the best way to ensure this is by *namespacing* them. That is,
    
  71.     by putting those static files inside *another* directory named for the
    
  72.     application itself.
    
  73. 
    
  74.     You can namespace static assets in :setting:`STATICFILES_DIRS` by
    
  75.     specifying :ref:`prefixes <staticfiles-dirs-prefixes>`.
    
  76. 
    
  77. .. _serving-static-files-in-development:
    
  78. 
    
  79. Serving static files during development
    
  80. =======================================
    
  81. 
    
  82. If you use :mod:`django.contrib.staticfiles` as explained above,
    
  83. :djadmin:`runserver` will do this automatically when :setting:`DEBUG` is set
    
  84. to ``True``. If you don't have ``django.contrib.staticfiles`` in
    
  85. :setting:`INSTALLED_APPS`, you can still manually serve static files using the
    
  86. :func:`django.views.static.serve` view.
    
  87. 
    
  88. This is not suitable for production use! For some common deployment
    
  89. strategies, see :doc:`/howto/static-files/deployment`.
    
  90. 
    
  91. For example, if your :setting:`STATIC_URL` is defined as ``static/``, you can
    
  92. do this by adding the following snippet to your ``urls.py``::
    
  93. 
    
  94.     from django.conf import settings
    
  95.     from django.conf.urls.static import static
    
  96. 
    
  97.     urlpatterns = [
    
  98.         # ... the rest of your URLconf goes here ...
    
  99.     ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    
  100. 
    
  101. .. note::
    
  102. 
    
  103.     This helper function works only in debug mode and only if
    
  104.     the given prefix is local (e.g. ``static/``) and not a URL (e.g.
    
  105.     ``http://static.example.com/``).
    
  106. 
    
  107.     Also this helper function only serves the actual :setting:`STATIC_ROOT`
    
  108.     folder; it doesn't perform static files discovery like
    
  109.     :mod:`django.contrib.staticfiles`.
    
  110. 
    
  111.     Finally, static files are served via a wrapper at the WSGI application
    
  112.     layer. As a consequence, static files requests do not pass through the
    
  113.     normal :doc:`middleware chain </topics/http/middleware>`.
    
  114. 
    
  115. .. _serving-uploaded-files-in-development:
    
  116. 
    
  117. Serving files uploaded by a user during development
    
  118. ===================================================
    
  119. 
    
  120. During development, you can serve user-uploaded media files from
    
  121. :setting:`MEDIA_ROOT` using the :func:`django.views.static.serve` view.
    
  122. 
    
  123. This is not suitable for production use! For some common deployment
    
  124. strategies, see :doc:`/howto/static-files/deployment`.
    
  125. 
    
  126. For example, if your :setting:`MEDIA_URL` is defined as ``media/``, you can do
    
  127. this by adding the following snippet to your :setting:`ROOT_URLCONF`::
    
  128. 
    
  129.     from django.conf import settings
    
  130.     from django.conf.urls.static import static
    
  131. 
    
  132.     urlpatterns = [
    
  133.         # ... the rest of your URLconf goes here ...
    
  134.     ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    
  135. 
    
  136. .. note::
    
  137. 
    
  138.     This helper function works only in debug mode and only if
    
  139.     the given prefix is local (e.g. ``media/``) and not a URL (e.g.
    
  140.     ``http://media.example.com/``).
    
  141. 
    
  142. .. _staticfiles-testing-support:
    
  143. 
    
  144. Testing
    
  145. =======
    
  146. 
    
  147. When running tests that use actual HTTP requests instead of the built-in
    
  148. testing client (i.e. when using the built-in :class:`LiveServerTestCase
    
  149. <django.test.LiveServerTestCase>`) the static assets need to be served along
    
  150. the rest of the content so the test environment reproduces the real one as
    
  151. faithfully as possible, but ``LiveServerTestCase`` has only very basic static
    
  152. file-serving functionality: It doesn't know about the finders feature of the
    
  153. ``staticfiles`` application and assumes the static content has already been
    
  154. collected under :setting:`STATIC_ROOT`.
    
  155. 
    
  156. Because of this, ``staticfiles`` ships its own
    
  157. :class:`django.contrib.staticfiles.testing.StaticLiveServerTestCase`, a subclass
    
  158. of the built-in one that has the ability to transparently serve all the assets
    
  159. during execution of these tests in a way very similar to what we get at
    
  160. development time with ``DEBUG = True``, i.e. without having to collect them
    
  161. using :djadmin:`collectstatic` first.
    
  162. 
    
  163. Deployment
    
  164. ==========
    
  165. 
    
  166. :mod:`django.contrib.staticfiles` provides a convenience management command
    
  167. for gathering static files in a single directory so you can serve them easily.
    
  168. 
    
  169. #. Set the :setting:`STATIC_ROOT` setting to the directory from which you'd
    
  170.    like to serve these files, for example::
    
  171. 
    
  172.        STATIC_ROOT = "/var/www/example.com/static/"
    
  173. 
    
  174. #. Run the :djadmin:`collectstatic` management command::
    
  175. 
    
  176.        $ python manage.py collectstatic
    
  177. 
    
  178.    This will copy all files from your static folders into the
    
  179.    :setting:`STATIC_ROOT` directory.
    
  180. 
    
  181. #. Use a web server of your choice to serve the
    
  182.    files. :doc:`/howto/static-files/deployment` covers some common deployment
    
  183.    strategies for static files.
    
  184. 
    
  185. Learn more
    
  186. ==========
    
  187. 
    
  188. This document has covered the basics and some common usage patterns. For
    
  189. complete details on all the settings, commands, template tags, and other pieces
    
  190. included in :mod:`django.contrib.staticfiles`, see :doc:`the staticfiles
    
  191. reference </ref/contrib/staticfiles>`.