Ticket #10119: use_index_html.diff

File use_index_html.diff, 3.8 KB (added by floguy, 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=None, show_indexes=False, use_index_html=False):
    2020    """
    2121    Serve static files below a given point in the directory structure.
    2222
     
    4949        return HttpResponseRedirect(newpath)
    5050    fullpath = os.path.join(document_root, newpath)
    5151    if os.path.isdir(fullpath):
    52         if show_indexes:
    53             return directory_index(newpath, fullpath)
    54         raise Http404, "Directory indexes are not allowed here."
     52        if use_index_html and os.path.exists(os.path.join(fullpath, 'index.html')):
     53            fullpath = os.path.join(fullpath, 'index.html')
     54        else:
     55            if show_indexes:
     56                return directory_index(newpath, fullpath)
     57            raise Http404, "Directory indexes are not allowed here."
    5558    if not os.path.exists(fullpath):
    5659        raise Http404, '"%s" does not exist' % fullpath
    5760    # Respect the If-Modified-Since header.
  • tests/regressiontests/views/media/index.html

     
     1<html>
     2    <head><title>Test Page</title></head>
     3    <body><p>This is a test.  This is only a test.</p></body>
     4</html>
     5 No newline at end of file
  • tests/regressiontests/views/tests/static.py

     
    2424        response = self.client.get('/views/site_media//%s' % file_name)
    2525        file = open(path.join(media_dir, file_name))
    2626        self.assertEquals(file.read(), response.content)
     27   
     28    def test_use_index_html(self):
     29        response = self.client.get('/views/site_media_index/')
     30        file = open(path.join(media_dir, 'index.html'))
     31        self.assertEquals(file.read(), response.content)
    2732
  • tests/regressiontests/views/urls.py

     
    3636
    3737    # Static views
    3838    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': media_dir}),
     39    (r'^site_media_index/(?P<path>.*)$', 'django.views.static.serve',
     40        {'document_root': media_dir, 'use_index_html': True}),
    3941)
    4042
    4143# Date-based generic views.
  • docs/howto/static-files.txt

     
    120120    the older (no extension) name, but it will prefer a the
    121121    ``directory_index.html`` version.
    122122
     123Automatically serving index.html
     124================================
     125
     126Optionally, you can pass the ``use_index_html`` parameter to the
     127:func:`~django.views.static.serve` view. This is ``False`` by default. If it's
     128``True``, and if the path refers to a directory, Django will look for a file
     129inside that directory called ``index.html``.  If it exists, that will be served.
     130If it does not exist, either a directory listing will be shown, or an HTTP error
     131code 404 will be raised--depending on whether you have enabled ``show_indexes``
     132or not.
     133
    123134Limiting use to DEBUG=True
    124135==========================
    125136
Back to Top