Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added docs/static_files.txt

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1454 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c0700db1a32d069fd84e9267e0664144b2af20db 1 parent 501b06c
Adrian Holovaty authored November 27, 2005

Showing 1 changed file with 81 additions and 0 deletions. Show diff stats Hide diff stats

  1. 81  docs/static_files.txt
81  docs/static_files.txt
... ...
@@ -0,0 +1,81 @@
  1
+=========================
  2
+How to serve static files
  3
+=========================
  4
+
  5
+Django itself doesn't serve static (media) files, such as images, style sheets,
  6
+or video. It leaves that job to whichever Web server you choose.
  7
+
  8
+The reasoning here is that standard Web servers, such as Apache_ and lighttpd_,
  9
+are much more fine-tuned at serving static files than a Web application
  10
+framework.
  11
+
  12
+With that said, Django does support static files **during development**. Use
  13
+the view ``django.views.static.serve`` to serve media files.
  14
+
  15
+.. _Apache: http://httpd.apache.org/
  16
+.. _lighttpd: http://www.lighttpd.net/
  17
+
  18
+The big, fat disclaimer
  19
+=======================
  20
+
  21
+Using this method is **inefficient** and **insecure**. Do not use this in a
  22
+production setting. Use this only for development.
  23
+
  24
+How to do it
  25
+============
  26
+
  27
+Just put this in your URLconf_::
  28
+
  29
+    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
  30
+
  31
+...where ``site_media`` is the URL where your media will be rooted, and
  32
+``/path/to/media`` is the filesystem root for your media.
  33
+
  34
+Examples:
  35
+
  36
+    * The file ``/path/to/media/foo.jpg`` will be made available at the URL
  37
+      ``/site_media/foo.jpg``.
  38
+
  39
+    * The file ``/path/to/media/css/mystyles.css`` will be made available
  40
+      at the URL ``/site_media/css/mystyles.css``.
  41
+
  42
+    * The file ``/path/bar.jpg`` will not be accessible, because it doesn't
  43
+      fall under the document root.
  44
+
  45
+.. _URLconf: http://www.djangoproject.com/documentation/url_dispatch/
  46
+
  47
+Limiting use to DEBUG=True
  48
+==========================
  49
+
  50
+Because URLconfs are just plain Python modules, you can use Python logic to
  51
+make the static-media view available only in development mode. This is a handy
  52
+trick to make sure the static-serving view doesn't slip into a production
  53
+setting by mistake.
  54
+
  55
+Do this by wrapping an ``if DEBUG`` statement around the
  56
+``django.views.static.serve`` inclusion. Here's a full example URLconf::
  57
+
  58
+    from django.conf.urls.defaults import *
  59
+    from django.conf.settings import DEBUG
  60
+
  61
+    urlpatterns = patterns('',
  62
+        (r'^/articles/2003/$', 'news.views.special_case_2003'),
  63
+        (r'^/articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
  64
+        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
  65
+        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),
  66
+    )
  67
+
  68
+    if DEBUG:
  69
+        urlpatterns += patterns('',
  70
+            (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
  71
+        )
  72
+
  73
+This code is straightforward. It imports the `DEBUG setting`_ and checks its
  74
+value. If it evaluates to ``True``, then ``site_media`` will be associated with
  75
+the ``django.views.static.serve`` view. If not (``DEBUG == False``), then the
  76
+view won't be made available.
  77
+
  78
+Of course, the catch here is that you'll have to remember to set ``DEBUG=False``
  79
+in your production settings file. But you should be doing that anyway.
  80
+
  81
+.. _DEBUG setting: http://www.djangoproject.com/documentation/settings/#debug

0 notes on commit c0700db

Please sign in to comment.
Something went wrong with that request. Please try again.