========================= How to serve static files ========================= Django itself doesn't serve static (media) files, such as images, style sheets, or video. It leaves that job to whichever Web server you choose. The reasoning here is that standard Web servers, such as Apache_ and lighttpd_, are much more fine-tuned at serving static files than a Web application framework. With that said, Django does support static files **during development**. Use the view ``django.views.static.serve`` to serve media files. .. _Apache: http://httpd.apache.org/ .. _lighttpd: http://www.lighttpd.net/ The big, fat disclaimer ======================= Using this method is **inefficient** and **insecure**. Do not use this in a production setting. Use this only for development. For information on serving static files in an Apache production environment, see the `Django mod_python documentation`_. .. _Django mod_python documentation: http://www.djangoproject.com/documentation/modpython/#serving-media-files How to do it ============ Just put this in your URLconf_:: (r'^site_media/(.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}), ...where ``site_media`` is the URL where your media will be rooted, and ``/path/to/media`` is the filesystem root for your media. You must pass a ``document_root`` parameter to indicate the filesystem root. Examples: * The file ``/path/to/media/foo.jpg`` will be made available at the URL ``/site_media/foo.jpg``. * The file ``/path/to/media/css/mystyles.css`` will be made available at the URL ``/site_media/css/mystyles.css``. * The file ``/path/bar.jpg`` will not be accessible, because it doesn't fall under the document root. .. _URLconf: http://www.djangoproject.com/documentation/url_dispatch/ Directory listings ================== Optionally, you can pass a ``show_indexes`` parameter to the ``static.serve`` view. This is ``False`` by default. If it's ``True``, Django will display file listings for directories. Example:: (r'^site_media/(.*)$', 'django.views.static.serve', {'document_root': '/path/to/media', 'show_indexes': True}), You can customize the index view by creating a template called ``static/directory_index``. That template gets two objects in its context: * ``directory`` -- the directory name (a string) * ``file_list`` -- a list of file names (as strings) in the directory Here's the default ``static/directory_index`` template::