index.txt 7.2 KB

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