Ticket #11495: 11495.diff
File 11495.diff, 3.4 KB (added by , 15 years ago) |
---|
-
django/views/static.py
16 16 from django.template import Template, Context, TemplateDoesNotExist 17 17 from django.utils.http import http_date 18 18 19 def serve(request, path, document_root =None, show_indexes=False):19 def serve(request, path, document_root, show_indexes=False, index_prefix=None): 20 20 """ 21 21 Serve static files below a given point in the directory structure. 22 22 23 To use, put a URL pattern such as::23 To use, put a URL pattern in your URLconf such as:: 24 24 25 (r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'}) 25 url(r'^(?P<path>.*)$', 'django.views.static.serve', 26 {'document_root': '/path/to/my/files/'}) 26 27 27 in your URLconf. You must provide the ``document_root`` param. You may 28 also set ``show_indexes`` to ``True`` if you'd like to serve a basic index 29 of the directory. This index view will use the template hardcoded below, 30 but if you'd like to override it, you can create a template called 31 ``static/directory_index.html``. 28 You must provide the ``document_root`` argument. 29 30 Set ``show_indexes`` to ``True`` to serve a basic index of directories. The 31 index view uses a hard-coded template by default. To use a custom index, 32 create a template called ``static/directory_index``. Use the 33 ``index_prefix`` argument to display the correct full path in indexes, for 34 example:: 35 36 url(r'^(?P<index_prefix>static/)(?P<path>.*)$', 37 'django.views.static.serve', 38 {'document_root': '/path/to/static/files/', 'show_indexes': True}) 32 39 """ 33 40 34 41 # Clean up given path to only allow serving files below document_root. … … 50 57 fullpath = os.path.join(document_root, newpath) 51 58 if os.path.isdir(fullpath): 52 59 if show_indexes: 53 return directory_index(newpath, fullpath )60 return directory_index(newpath, fullpath, index_prefix) 54 61 raise Http404, "Directory indexes are not allowed here." 55 62 if not os.path.exists(fullpath): 56 63 raise Http404, '"%s" does not exist' % fullpath … … 89 96 </html> 90 97 """ 91 98 92 def directory_index(path, fullpath ):99 def directory_index(path, fullpath, prefix=None): 93 100 try: 94 101 t = loader.select_template(['static/directory_index.html', 95 102 'static/directory_index']) 96 103 except TemplateDoesNotExist: 97 t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default directory index template') 104 t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, 105 name='Default directory index template') 98 106 files = [] 107 dirs = [] 99 108 for f in os.listdir(fullpath): 100 109 if not f.startswith('.'): 101 110 if os.path.isdir(os.path.join(fullpath, f)): 102 f += '/' 103 files.append(f) 111 dirs.append('%s/' % f) 112 else: 113 files.append(f) 114 dirs.sort() 115 files.sort() 116 directory = '%s%s' % (prefix or '', path) 117 if not directory.endswith('/'): 118 directory = '%s/' % directory 104 119 c = Context({ 105 'directory' : path + '/',106 'file_list' : files,120 'directory' : directory, 121 'file_list' : dirs + files, 107 122 }) 108 123 return HttpResponse(t.render(c)) 109 124