django/django · warning · Http404

Directory indexes are not allowed here.

Error message

Directory indexes are not allowed here.

What it means

Raised as Http404 from staticfiles serve() view when the requested path maps to a directory (path ends with '/' or is empty) and finders cannot resolve it to a file. The staticfiles dev view does not support directory listing, so a directory request is rejected with a 404 carrying this message rather than listing contents.

Source

Thrown at django/contrib/staticfiles/views.py:37

    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.
    """
    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)

View on GitHub (pinned to b5388a3a80)

Solutions

  1. Request a specific file, not a directory (remove trailing slash and add a filename).
  2. If you need directory listings, serve files another way (staticfiles dev view intentionally disallows it).
  3. Correct the URL-building code so it always targets a real asset.

Example fix

<!-- before -->
<img src="{% static 'images/' %}">  <!-- directory -> 404 -->

<!-- after -->
<img src="{% static 'images/logo.png' %}">
Defensive patterns

Strategy: try-catch

Validate before calling

from django.contrib.staticfiles import finders

path = request.path.strip("/")
if not path or path.endswith("/"):
    raise ValueError("Directory listing not supported")
if not finders.find(path.rstrip("/")):
    raise ValueError("Static file not found")

Try / catch

from django.http import Http404

try:
    response = serve(request, path)
except Http404 as e:
    logger.info("Static not found: %s", e)
    raise

Prevention

When it happens

Trigger: A request to the static serve URL with a trailing slash or empty path, in DEBUG=True (or with insecure=True), where finders.find(normalized_path) returns None. E.g. GET /static/css/ resolves to nothing.

Common situations: Browsing to a static directory in the dev server expecting an index; a template/JS building a URL that ends in '/' pointing at a folder; a typo that drops the filename.

Related errors


AI-assisted analysis of django/django@b5388a3a80 (2026-08-10). Data as JSON: /api/errors/cd40915d7c4bf072. Report an issue: GitHub.