index.txt 7.4 KB

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