1 | ===================================
|
---|
2 | Outputting static files with Django
|
---|
3 | ===================================
|
---|
4 |
|
---|
5 | This document explains how to serve static files using Django.
|
---|
6 |
|
---|
7 | Django provides you with an easy to use view to serve static content
|
---|
8 | such as images or ``CSS`` files.
|
---|
9 |
|
---|
10 | .. admonition:: Note
|
---|
11 |
|
---|
12 | Add some warning about using this only in development or whatever your
|
---|
13 | policy will be on this.
|
---|
14 |
|
---|
15 | See `the Django overview`_ for a quick introduction to URL configurations; this
|
---|
16 | document will continue from there.
|
---|
17 |
|
---|
18 | .. _`the Django overview`: http://www.djangoproject.com/documentation/overview/#design-your-urls
|
---|
19 |
|
---|
20 | The static view
|
---|
21 | ===============
|
---|
22 |
|
---|
23 | Here's the example from that overview::
|
---|
24 |
|
---|
25 | from django.conf.urls.defaults import *
|
---|
26 |
|
---|
27 | urlpatterns = patterns('',
|
---|
28 | (r'^/articles/(?P<year>\d{4})/$', 'myproject.news.views.articles.year_archive'),
|
---|
29 | (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.articles.month_archive'),
|
---|
30 | (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.articles.article_detail'),
|
---|
31 | )
|
---|
32 |
|
---|
33 | To serve static content, create a directory in a location of your choice
|
---|
34 | to contain all your desired files and subdirectories. Now modify your
|
---|
35 | URLconf similar to the following::
|
---|
36 |
|
---|
37 | urlpatterns = patterns('',
|
---|
38 | (r'^/articles/(?P<year>\d{4})/$', 'myproject.news.views.articles.year_archive'),
|
---|
39 | (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.articles.month_archive'),
|
---|
40 | (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.articles.article_detail'),
|
---|
41 | (r'^media/(?P<path>.*)', 'django.views.static.serve', {'document_root' : '/path/to/your/files/', 'show_indexes':True}),
|
---|
42 | )
|
---|
43 |
|
---|
44 | This will serve static content from your chosen directory and its subdirectories
|
---|
45 | for all URLs beginning with /media. You must provide the ``document_root`` parameter
|
---|
46 | to indicate where these files are to be found. The ``show_indexes`` parameter
|
---|
47 | is optional, but if set to ``True``, the view will show a basic index of all the
|
---|
48 | files in a given directory.
|
---|
49 |
|
---|
50 | If you would like to create your own template to customize the index view,
|
---|
51 | you may create a template named ``static/directory_index``.
|
---|
52 |
|
---|
53 | This example of a ``static/directory_index`` template is identical to the default
|
---|
54 | incuded with Django::
|
---|
55 |
|
---|
56 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
---|
57 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
---|
58 | <head>
|
---|
59 | <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
|
---|
60 | <meta http-equiv="Content-Language" content="en-us" />
|
---|
61 | <title>Index of {{ directory }}</title>
|
---|
62 | </head>
|
---|
63 | <body>
|
---|
64 | <h1>Index of {{ directory }}</h1>
|
---|
65 | <ul>
|
---|
66 | {% for f in file_list %}
|
---|
67 | <li><a href="{{ f }}">{{ f }}</a></li>
|
---|
68 | {% endfor %}
|
---|
69 | </ul>
|
---|
70 | </body>
|
---|
71 | </html>
|
---|
72 |
|
---|