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