Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #428 -- added a "static pages" view. Note that this view should…

… only be used for testing!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@683 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 572ac3e7dfadec6434527ebbdccef97de1cc191c 1 parent c05cce5
Jacob Kaplan-Moss authored September 25, 2005

Showing 1 changed file with 86 additions and 0 deletions. Show diff stats Hide diff stats

  1. 86  django/views/static.py
86  django/views/static.py
... ...
@@ -0,0 +1,86 @@
  1
+import os
  2
+import urllib
  3
+import posixpath
  4
+import mimetypes
  5
+from django.core import template_loader
  6
+from django.core.exceptions import Http404, ImproperlyConfigured
  7
+from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
  8
+from django.core.template import Template, Context, TemplateDoesNotExist
  9
+
  10
+def serve(request, path, document_root=None, show_indexes=False):
  11
+    """
  12
+    Serve static files below a given point in the directory structure.
  13
+    
  14
+    To use, put a URL pattern like::
  15
+        
  16
+        (r'^(?P<path>).*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'})
  17
+     
  18
+    in your URL conf; you must provide the ``document_root`` param.  You may
  19
+    also set ``show_indexes`` to ``True`` if you'd like to serve a basic index
  20
+    of the directory.  This index view will use the template hardcoded below,
  21
+    but if you'd like to override it, you can create a template called
  22
+    ``static/directory_index``.
  23
+    """
  24
+    
  25
+    # Clean up given path to only allow serving files below document_root.
  26
+    path = posixpath.normpath(urllib.unquote(path))
  27
+    newpath = ''
  28
+    for part in path.split('/'):
  29
+        if not part:
  30
+            # strip empty path components
  31
+            continue    
  32
+        drive, part = os.path.splitdrive(part)
  33
+        head, part = os.path.split(part)
  34
+        if part in (os.curdir, os.pardir):
  35
+            # strip '.' amd '..' in path
  36
+            continue
  37
+        newpath = os.path.join(newpath, part)
  38
+    if newpath and path != newpath:
  39
+        return HttpResponseRedirect(newpath)
  40
+    fullpath = os.path.join(document_root, newpath)
  41
+    if os.path.isdir(fullpath):
  42
+        if show_indexes:
  43
+            return directory_index(newpath, fullpath)
  44
+        else:
  45
+            raise Http404, "Directory indexes are not allowed here."
  46
+    elif not os.path.exists(fullpath):
  47
+        raise Http404, '"%s" does not exist' % fullpath
  48
+    else:
  49
+        mimetype = mimetypes.guess_type(fullpath)[0]
  50
+        return HttpResponse(open(fullpath).read(), mimetype=mimetype)
  51
+
  52
+DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
  53
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  54
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  55
+  <head>
  56
+    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  57
+    <meta http-equiv="Content-Language" content="en-us" />
  58
+    <title>Index of {{ directory }}</title>
  59
+  </head>
  60
+  <body>
  61
+    <h1>Index of {{ directory }}</h1>
  62
+    <ul>
  63
+      {% for f in file_list %}
  64
+      <li><a href="{{ f }}">{{ f }}</a></li>
  65
+      {% endfor %}
  66
+    </ul>
  67
+  </body>
  68
+</html>
  69
+"""
  70
+        
  71
+def directory_index(path, fullpath):
  72
+    try:
  73
+        t = template_loader.get_template('static/directory_index')
  74
+    except TemplateDoesNotExist:
  75
+        t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
  76
+    files = []
  77
+    for f in os.listdir(fullpath):
  78
+        if not f.startswith('.'):
  79
+            if os.path.isdir(os.path.join(fullpath, f)):
  80
+                f += '/'
  81
+            files.append(f)
  82
+    c = Context({
  83
+        'directory' : path + '/',
  84
+        'file_list' : files,
  85
+    })
  86
+    return HttpResponse(t.render(c))

0 notes on commit 572ac3e

Please sign in to comment.
Something went wrong with that request. Please try again.