2008-08-24 06:25:40 +08:00
|
|
|
.. _howto-static-files:
|
|
|
|
|
2005-11-27 22:42:21 +08:00
|
|
|
=========================
|
|
|
|
How to serve static files
|
|
|
|
=========================
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. module:: django.views.static
|
|
|
|
:synopsis: Serving of static files during development.
|
2008-12-08 13:35:20 +08:00
|
|
|
|
2005-11-27 22:42:21 +08:00
|
|
|
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.
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
The reasoning here is that standard Web servers, such as Apache_, lighttpd_ and
|
|
|
|
Cherokee_, are much more fine-tuned at serving static files than a Web
|
|
|
|
application framework.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
With that said, Django does support static files **during development**. You can
|
|
|
|
use the :func:`django.views.static.serve` view to serve media files.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
|
|
|
.. _Apache: http://httpd.apache.org/
|
|
|
|
.. _lighttpd: http://www.lighttpd.net/
|
2009-04-01 07:34:03 +08:00
|
|
|
.. _Cherokee: http://www.cherokee-project.com/
|
2005-11-27 22:42:21 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
If you just need to serve the admin media from a nonstandard location, see
|
|
|
|
the :djadminopt:`--adminmedia` parameter to :djadmin:`runserver`.
|
|
|
|
|
2005-11-27 22:42:21 +08:00
|
|
|
The big, fat disclaimer
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Using this method is **inefficient** and **insecure**. Do not use this in a
|
|
|
|
production setting. Use this only for development.
|
|
|
|
|
2005-11-27 22:49:57 +08:00
|
|
|
For information on serving static files in an Apache production environment,
|
2008-08-24 06:25:40 +08:00
|
|
|
see the :ref:`Django mod_python documentation <serving-media-files>`.
|
2005-11-27 22:49:57 +08:00
|
|
|
|
2005-11-27 22:42:21 +08:00
|
|
|
How to do it
|
|
|
|
============
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Here's the formal definition of the :func:`~django.views.static.serve` view:
|
|
|
|
|
|
|
|
.. function:: def serve(request, path, document_root, show_indexes=False):
|
|
|
|
|
|
|
|
To use it, just put this in your :ref:`URLconf <topics-http-urls>`::
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2008-10-06 16:29:05 +08:00
|
|
|
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
|
|
|
|
{'document_root': '/path/to/media'}),
|
2005-11-27 22:42:21 +08:00
|
|
|
|
|
|
|
...where ``site_media`` is the URL where your media will be rooted, and
|
2008-08-24 06:25:40 +08:00
|
|
|
``/path/to/media`` is the filesystem root for your media. This will call the
|
|
|
|
:func:`~django.views.static.serve` view, passing in the path from the URLconf
|
|
|
|
and the (required) ``document_root`` parameter.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Given the above URLconf:
|
2005-11-27 22:42:21 +08:00
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
2008-10-06 16:29:05 +08:00
|
|
|
Of course, it's not compulsory to use a fixed string for the
|
|
|
|
``'document_root'`` value. You might wish to make that an entry in your
|
|
|
|
settings file and use the setting value there. That will allow you and
|
|
|
|
other developers working on the code to easily change the value as
|
|
|
|
required. For example, if we have a line in ``settings.py`` that says::
|
|
|
|
|
|
|
|
STATIC_DOC_ROOT = '/path/to/media'
|
|
|
|
|
|
|
|
...we could write the above :ref:`URLconf <topics-http-urls>` entry as::
|
|
|
|
|
2008-12-08 13:35:20 +08:00
|
|
|
from django.conf import settings
|
|
|
|
...
|
2008-10-06 16:29:05 +08:00
|
|
|
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
|
|
|
|
{'document_root': settings.STATIC_DOC_ROOT}),
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2009-04-01 07:34:03 +08:00
|
|
|
Be careful not to use the same path as your :setting:`ADMIN_MEDIA_PREFIX` (which defaults
|
|
|
|
to ``/media/``) as this will overwrite your URLconf entry.
|
|
|
|
|
2005-11-27 23:28:19 +08:00
|
|
|
Directory listings
|
|
|
|
==================
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Optionally, you can pass the ``show_indexes`` parameter to the
|
|
|
|
:func:`~django.views.static.serve` view. This is ``False`` by default. If it's
|
|
|
|
``True``, Django will display file listings for directories.
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
For example::
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2008-10-06 16:29:05 +08:00
|
|
|
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
|
|
|
|
{'document_root': '/path/to/media', 'show_indexes': True}),
|
2005-11-27 23:28:19 +08:00
|
|
|
|
|
|
|
You can customize the index view by creating a template called
|
2008-08-24 06:25:40 +08:00
|
|
|
``static/directory_index.html``. That template gets two objects in its context:
|
2005-11-27 23:28:19 +08:00
|
|
|
|
|
|
|
* ``directory`` -- the directory name (a string)
|
|
|
|
* ``file_list`` -- a list of file names (as strings) in the directory
|
|
|
|
|
2009-01-09 20:41:07 +08:00
|
|
|
Here's the default ``static/directory_index.html`` template:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-27 23:28:19 +08:00
|
|
|
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
|
|
|
|
<meta http-equiv="Content-Language" content="en-us" />
|
|
|
|
<meta name="robots" content="NONE,NOARCHIVE" />
|
|
|
|
<title>Index of {{ directory }}</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Index of {{ directory }}</h1>
|
|
|
|
<ul>
|
|
|
|
{% for f in file_list %}
|
|
|
|
<li><a href="{{ f }}">{{ f }}</a></li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
2009-01-09 20:41:07 +08:00
|
|
|
.. versionchanged:: 1.0.3
|
|
|
|
Prior to Django 1.0.3, there was a bug in the view that provided directory
|
|
|
|
listings. The template that was loaded had to be called
|
|
|
|
``static/directory_listing`` (with no ``.html`` extension). For backwards
|
|
|
|
compatibility with earlier versions, Django will still load templates with
|
2009-09-13 06:48:20 +08:00
|
|
|
the older (no extension) name, but it will prefer the
|
2009-01-09 20:41:07 +08:00
|
|
|
``directory_index.html`` version.
|
|
|
|
|
2005-11-27 22:42:21 +08:00
|
|
|
Limiting use to DEBUG=True
|
|
|
|
==========================
|
|
|
|
|
|
|
|
Because URLconfs are just plain Python modules, you can use Python logic to
|
|
|
|
make the static-media view available only in development mode. This is a handy
|
|
|
|
trick to make sure the static-serving view doesn't slip into a production
|
|
|
|
setting by mistake.
|
|
|
|
|
|
|
|
Do this by wrapping an ``if DEBUG`` statement around the
|
2008-08-24 06:25:40 +08:00
|
|
|
:func:`django.views.static.serve` inclusion. Here's a full example URLconf::
|
2005-11-27 22:42:21 +08:00
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2005-11-27 22:42:21 +08:00
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
2007-07-10 10:34:42 +08:00
|
|
|
(r'^articles/2003/$', 'news.views.special_case_2003'),
|
|
|
|
(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
|
|
|
|
(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
|
|
|
|
(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),
|
2005-11-27 22:42:21 +08:00
|
|
|
)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
if settings.DEBUG:
|
2005-11-27 22:42:21 +08:00
|
|
|
urlpatterns += patterns('',
|
|
|
|
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
|
|
|
|
)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
This code is straightforward. It imports the settings and checks the value of
|
2008-08-24 06:25:40 +08:00
|
|
|
the :setting:`DEBUG` setting. If it evaluates to ``True``, then ``site_media``
|
|
|
|
will be associated with the ``django.views.static.serve`` view. If not, then the
|
|
|
|
view won't be made available.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
|
|
|
Of course, the catch here is that you'll have to remember to set ``DEBUG=False``
|
|
|
|
in your production settings file. But you should be doing that anyway.
|