static-files.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. =========================
  2. How to serve static files
  3. =========================
  4. .. module:: django.views.static
  5. :synopsis: Serving of static files during development.
  6. Django itself doesn't serve static (media) files, such as images, style sheets,
  7. or video. It leaves that job to whichever Web server you choose.
  8. The reasoning here is that standard Web servers, such as Apache_, lighttpd_ and
  9. Cherokee_, are much more fine-tuned at serving static files than a Web
  10. application framework.
  11. With that said, Django does support static files **during development**. You can
  12. use the :func:`django.views.static.serve` view to serve media files.
  13. .. _Apache: http://httpd.apache.org/
  14. .. _lighttpd: http://www.lighttpd.net/
  15. .. _Cherokee: http://www.cherokee-project.com/
  16. .. seealso::
  17. If you just need to serve the admin media from a nonstandard location, see
  18. the :djadminopt:`--adminmedia` parameter to :djadmin:`runserver`.
  19. The big, fat disclaimer
  20. =======================
  21. Using this method is **inefficient** and **insecure**. Do not use this in a
  22. production setting. Use this only for development.
  23. For information on serving static files in an Apache production environment,
  24. see the :ref:`Django mod_python documentation <serving-media-files>`.
  25. How to do it
  26. ============
  27. Here's the formal definition of the :func:`~django.views.static.serve` view:
  28. .. function:: def serve(request, path, document_root, show_indexes=False)
  29. To use it, just put this in your :doc:`URLconf </topics/http/urls>`::
  30. (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
  31. {'document_root': '/path/to/media'}),
  32. ...where ``site_media`` is the URL where your media will be rooted, and
  33. ``/path/to/media`` is the filesystem root for your media. This will call the
  34. :func:`~django.views.static.serve` view, passing in the path from the URLconf
  35. and the (required) ``document_root`` parameter.
  36. Given the above URLconf:
  37. * The file ``/path/to/media/foo.jpg`` will be made available at the URL
  38. ``/site_media/foo.jpg``.
  39. * The file ``/path/to/media/css/mystyles.css`` will be made available
  40. at the URL ``/site_media/css/mystyles.css``.
  41. * The file ``/path/bar.jpg`` will not be accessible, because it doesn't
  42. fall under the document root.
  43. Of course, it's not compulsory to use a fixed string for the
  44. ``'document_root'`` value. You might wish to make that an entry in your
  45. settings file and use the setting value there. That will allow you and
  46. other developers working on the code to easily change the value as
  47. required. For example, if we have a line in ``settings.py`` that says::
  48. STATIC_DOC_ROOT = '/path/to/media'
  49. ...we could write the above :doc:`URLconf </topics/http/urls>` entry as::
  50. from django.conf import settings
  51. ...
  52. (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
  53. {'document_root': settings.STATIC_DOC_ROOT}),
  54. Be careful not to use the same path as your :setting:`ADMIN_MEDIA_PREFIX` (which defaults
  55. to ``/media/``) as this will overwrite your URLconf entry.
  56. Directory listings
  57. ==================
  58. Optionally, you can pass the ``show_indexes`` parameter to the
  59. :func:`~django.views.static.serve` view. This is ``False`` by default. If it's
  60. ``True``, Django will display file listings for directories.
  61. For example::
  62. (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
  63. {'document_root': '/path/to/media', 'show_indexes': True}),
  64. You can customize the index view by creating a template called
  65. ``static/directory_index.html``. That template gets two objects in its context:
  66. * ``directory`` -- the directory name (a string)
  67. * ``file_list`` -- a list of file names (as strings) in the directory
  68. Here's the default ``static/directory_index.html`` template:
  69. .. code-block:: html+django
  70. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  71. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  72. <head>
  73. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  74. <meta http-equiv="Content-Language" content="en-us" />
  75. <meta name="robots" content="NONE,NOARCHIVE" />
  76. <title>Index of {{ directory }}</title>
  77. </head>
  78. <body>
  79. <h1>Index of {{ directory }}</h1>
  80. <ul>
  81. {% for f in file_list %}
  82. <li><a href="{{ f }}">{{ f }}</a></li>
  83. {% endfor %}
  84. </ul>
  85. </body>
  86. </html>
  87. .. versionchanged:: 1.0.3
  88. Prior to Django 1.0.3, there was a bug in the view that provided directory
  89. listings. The template that was loaded had to be called
  90. ``static/directory_listing`` (with no ``.html`` extension). For backwards
  91. compatibility with earlier versions, Django will still load templates with
  92. the older (no extension) name, but it will prefer the
  93. ``directory_index.html`` version.
  94. Limiting use to DEBUG=True
  95. ==========================
  96. Because URLconfs are just plain Python modules, you can use Python logic to
  97. make the static-media view available only in development mode. This is a handy
  98. trick to make sure the static-serving view doesn't slip into a production
  99. setting by mistake.
  100. Do this by wrapping an ``if DEBUG`` statement around the
  101. :func:`django.views.static.serve` inclusion. Here's a full example URLconf::
  102. from django.conf.urls.defaults import *
  103. from django.conf import settings
  104. urlpatterns = patterns('',
  105. (r'^articles/2003/$', 'news.views.special_case_2003'),
  106. (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
  107. (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
  108. (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),
  109. )
  110. if settings.DEBUG:
  111. urlpatterns += patterns('',
  112. (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
  113. )
  114. This code is straightforward. It imports the settings and checks the value of
  115. the :setting:`DEBUG` setting. If it evaluates to ``True``, then ``site_media``
  116. will be associated with the ``django.views.static.serve`` view. If not, then the
  117. view won't be made available.
  118. Of course, the catch here is that you'll have to remember to set ``DEBUG=False``
  119. in your production settings file. But you should be doing that anyway.