PageRenderTime 37ms CodeModel.GetById 32ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/staticfiles/views.py

https://code.google.com/p/mango-py/
Python | 39 lines | 25 code | 3 blank | 11 comment | 0 complexity | cb36b3affc66a8bd371712b22c200e71 MD5 | raw file
 1"""
 2Views and functions for serving static files. These are only to be used during
 3development, and SHOULD NOT be used in a production setting.
 4
 5"""
 6import os
 7import posixpath
 8import urllib
 9
10from django.conf import settings
11from django.core.exceptions import ImproperlyConfigured
12from django.http import Http404
13from django.views import static
14
15from django.contrib.staticfiles import finders
16
17def serve(request, path, document_root=None, insecure=False, **kwargs):
18    """
19    Serve static files below a given point in the directory structure or
20    from locations inferred from the staticfiles finders.
21
22    To use, put a URL pattern such as::
23
24        (r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve')
25
26    in your URLconf.
27
28    It uses the django.views.static view to serve the found files.
29    """
30    if not settings.DEBUG and not insecure:
31        raise ImproperlyConfigured("The staticfiles view can only be used in "
32                                   "debug mode or if the the --insecure "
33                                   "option of 'runserver' is used")
34    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
35    absolute_path = finders.find(normalized_path)
36    if not absolute_path:
37        raise Http404("'%s' could not be found" % path)
38    document_root, path = os.path.split(absolute_path)
39    return static.serve(request, path, document_root=document_root, **kwargs)