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.
 
 
 
 

41 lines
1.3 KiB

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