Ticket #10119: use_index_html.diff
File use_index_html.diff, 3.8 KB (added by , 16 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=None, show_indexes=False, use_index_html=False): 20 20 """ 21 21 Serve static files below a given point in the directory structure. 22 22 … … 49 49 return HttpResponseRedirect(newpath) 50 50 fullpath = os.path.join(document_root, newpath) 51 51 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." 55 58 if not os.path.exists(fullpath): 56 59 raise Http404, '"%s" does not exist' % fullpath 57 60 # 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
24 24 response = self.client.get('/views/site_media//%s' % file_name) 25 25 file = open(path.join(media_dir, file_name)) 26 26 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) 27 32 -
tests/regressiontests/views/urls.py
36 36 37 37 # Static views 38 38 (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}), 39 41 ) 40 42 41 43 # Date-based generic views. -
docs/howto/static-files.txt
120 120 the older (no extension) name, but it will prefer a the 121 121 ``directory_index.html`` version. 122 122 123 Automatically serving index.html 124 ================================ 125 126 Optionally, 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 129 inside that directory called ``index.html``. If it exists, that will be served. 130 If it does not exist, either a directory listing will be shown, or an HTTP error 131 code 404 will be raised--depending on whether you have enabled ``show_indexes`` 132 or not. 133 123 134 Limiting use to DEBUG=True 124 135 ========================== 125 136