diff --git a/debug_toolbar/panels/staticfiles.py b/debug_toolbar/panels/staticfiles.py index b0997404c..3dd29e979 100644 --- a/debug_toolbar/panels/staticfiles.py +++ b/debug_toolbar/panels/staticfiles.py @@ -17,8 +17,9 @@ class StaticFile: Representing the different properties of a static file. """ - def __init__(self, path): + def __init__(self, *, path, url): self.path = path + self._url = url def __str__(self): return self.path @@ -27,7 +28,7 @@ def real_path(self): return finders.find(self.path) def url(self): - return storage.staticfiles_storage.url(self.path) + return self._url # This will record and map the StaticFile instances with its associated @@ -58,6 +59,7 @@ def _setup(self): class DebugStaticFilesStorage(configured_storage_cls): def url(self, path): + url = super().url(path) with contextlib.suppress(LookupError): # For LookupError: # The ContextVar wasn't set yet. Since the toolbar wasn't properly @@ -66,10 +68,10 @@ def url(self, path): request_id = request_id_context_var.get() record_static_file_signal.send( sender=self, - staticfile=StaticFile(path), + staticfile=StaticFile(path=str(path), url=url), request_id=request_id, ) - return super().url(path) + return url self._wrapped = DebugStaticFilesStorage() diff --git a/docs/changes.rst b/docs/changes.rst index 6d37b065a..b5d9a5b50 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -7,6 +7,7 @@ Pending * Added Python 3.13 to the CI matrix. * Removed support for Python 3.8 as it has reached end of life. * Converted to Django Commons PyPI release process. +* Fixed a crash which occurred when using non-``str`` static file values. 5.0.0-alpha (2024-09-01) ------------------------ diff --git a/tests/panels/test_staticfiles.py b/tests/panels/test_staticfiles.py index 3caedc4eb..334b0b6a3 100644 --- a/tests/panels/test_staticfiles.py +++ b/tests/panels/test_staticfiles.py @@ -1,7 +1,9 @@ +from pathlib import Path + from django.conf import settings from django.contrib.staticfiles import finders from django.shortcuts import render -from django.test import AsyncRequestFactory +from django.test import AsyncRequestFactory, RequestFactory from ..base import BaseTestCase @@ -58,3 +60,19 @@ def test_insert_content(self): "django.contrib.staticfiles.finders.AppDirectoriesFinder", content ) self.assertValidHTML(content) + + def test_path(self): + def get_response(request): + # template contains one static file + return render( + request, + "staticfiles/path.html", + {"path": Path("additional_static/base.css")}, + ) + + self._get_response = get_response + request = RequestFactory().get("/") + response = self.panel.process_request(request) + self.panel.generate_stats(self.request, response) + self.assertEqual(self.panel.num_used, 1) + self.assertIn('"/static/additional_static/base.css"', self.panel.content) diff --git a/tests/templates/staticfiles/path.html b/tests/templates/staticfiles/path.html new file mode 100644 index 000000000..bf3781c3b --- /dev/null +++ b/tests/templates/staticfiles/path.html @@ -0,0 +1 @@ +{% load static %}{% static path %}