Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nice 404 error message #366

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

Pending
-------

* A nicer and more helpful error page in debug instead of just a 404 (Django only)


6.0.0 (2022-02-10)
------------------

Expand Down
16 changes: 16 additions & 0 deletions src/whitenoise/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.urls import get_script_prefix

from .base import WhiteNoise
from .responders import MissingFileError
from .string_utils import decode_if_byte_string, ensure_leading_trailing_slash

__all__ = ["WhiteNoiseMiddleware"]
Expand Down Expand Up @@ -68,6 +69,21 @@ def process_request(self, request):
if static_file is not None:
return self.serve(static_file, request)

if settings.DEBUG and request.path.startswith(settings.STATIC_URL):
from django.contrib.staticfiles.finders import get_finders

finders = get_finders()
app_dirs = []
for finder in finders:
for storage in finder.storages.values():
app_dirs.append(storage.location)
app_dirs = "\n ".join(sorted(app_dirs))
raise MissingFileError(
f"""{request.path} not found. Searched these paths:

{app_dirs}"""
)

@staticmethod
def serve(static_file, request):
response = static_file.get_response(request.method, request.META)
Expand Down
9 changes: 8 additions & 1 deletion tests/django_urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from __future__ import annotations

urlpatterns = []
from django.urls import path


def avoid_django_default_welcome_page():
pass


urlpatterns = [path("", avoid_django_default_welcome_page)]
19 changes: 19 additions & 0 deletions tests/test_django_whitenoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import tempfile
from contextlib import closing
from pathlib import Path
from urllib.parse import urljoin, urlparse

import django
Expand Down Expand Up @@ -210,3 +211,21 @@ def test_relative_static_url(server, static_files, _collect_static):
url = storage.staticfiles_storage.url(static_files.js_path)
response = server.get(url)
assert response.content == static_files.js_content


def test_404_in_prod(server):
response = server.get(settings.STATIC_URL + "garbage")
assert response.status_code == 404


@override_settings(DEBUG=True)
def test_error_message(server):
response = server.get(settings.STATIC_URL + "garbage")
print(response.content.decode())
app_dirs = Path(__file__).parent / "test_files" / "static"

expected = f"""{settings.STATIC_URL + 'garbage'} not found. Searched these paths:

{app_dirs}"""

assert expected in str(response.content.decode())