views and functions for serving static files these are only to be used during development and should not be used in a production setting serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders to use put a url pattern such as from django contrib staticfiles import views path path path views serve in your urlconf it uses the django views static serve view to serve the found files serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders to use put a url pattern such as from django contrib staticfiles import views path path path views serve in your urlconf it uses the django views static serve view to serve the found files
import os
import posixpath

from django.conf import settings
from django.contrib.staticfiles import finders
from django.http import Http404
from django.views import static


def serve(request, path, insecure=False, **kwargs):
    
    if not settings.DEBUG and not insecure:
        raise Http404
    normalized_path = posixpath.normpath(path).lstrip("/")
    absolute_path = finders.find(normalized_path)
    if not absolute_path:
        if path.endswith("/") or path == "":
            raise Http404("Directory indexes are not allowed here.")
        raise Http404("'%s' could not be found" % path)
    document_root, path = os.path.split(absolute_path)
    return static.serve(request, path, document_root=document_root, **kwargs)