You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. from django.conf import settings
  8. from django.contrib.staticfiles import finders
  9. from django.http import Http404
  10. from django.views import static
  11. def serve(request, path, insecure=False, **kwargs):
  12. """
  13. Serve static files below a given point in the directory structure or
  14. from locations inferred from the staticfiles finders.
  15. To use, put a URL pattern such as::
  16. from django.contrib.staticfiles import views
  17. url(r'^(?P<path>.*)$', views.serve)
  18. in your URLconf.
  19. It uses the django.views.static.serve() view to serve the found files.
  20. """
  21. if not settings.DEBUG and not insecure:
  22. raise Http404
  23. normalized_path = posixpath.normpath(path).lstrip('/')
  24. absolute_path = finders.find(normalized_path)
  25. if not absolute_path:
  26. if path.endswith('/') or path == '':
  27. raise Http404("Directory indexes are not allowed here.")
  28. raise Http404("'%s' could not be found" % path)
  29. document_root, path = os.path.split(absolute_path)
  30. return static.serve(request, path, document_root=document_root, **kwargs)