Ticket #11495: 11495.diff

File 11495.diff, 3.4 KB (added by Chris Beaven, 15 years ago)
  • django/views/static.py

     
    1616from django.template import Template, Context, TemplateDoesNotExist
    1717from django.utils.http import http_date
    1818
    19 def serve(request, path, document_root=None, show_indexes=False):
     19def serve(request, path, document_root, show_indexes=False, index_prefix=None):
    2020    """
    2121    Serve static files below a given point in the directory structure.
    2222
    23     To use, put a URL pattern such as::
     23    To use, put a URL pattern in your URLconf such as::
    2424
    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/'})
    2627
    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})
    3239    """
    3340
    3441    # Clean up given path to only allow serving files below document_root.
     
    5057    fullpath = os.path.join(document_root, newpath)
    5158    if os.path.isdir(fullpath):
    5259        if show_indexes:
    53             return directory_index(newpath, fullpath)
     60            return directory_index(newpath, fullpath, index_prefix)
    5461        raise Http404, "Directory indexes are not allowed here."
    5562    if not os.path.exists(fullpath):
    5663        raise Http404, '"%s" does not exist' % fullpath
     
    8996</html>
    9097"""
    9198
    92 def directory_index(path, fullpath):
     99def directory_index(path, fullpath, prefix=None):
    93100    try:
    94101        t = loader.select_template(['static/directory_index.html',
    95102                'static/directory_index'])
    96103    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')
    98106    files = []
     107    dirs = []
    99108    for f in os.listdir(fullpath):
    100109        if not f.startswith('.'):
    101110            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
    104119    c = Context({
    105         'directory' : path + '/',
    106         'file_list' : files,
     120        'directory' : directory,
     121        'file_list' : dirs + files,
    107122    })
    108123    return HttpResponse(t.render(c))
    109124
Back to Top