Skip to content

Commit 6d45d1d

Browse files
authored
The static files panel shouldn't choke on unexpected data types (#2021)
1 parent 89449b6 commit 6d45d1d

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

debug_toolbar/panels/staticfiles.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class StaticFile:
1717
Representing the different properties of a static file.
1818
"""
1919

20-
def __init__(self, path):
20+
def __init__(self, *, path, url):
2121
self.path = path
22+
self._url = url
2223

2324
def __str__(self):
2425
return self.path
@@ -27,7 +28,7 @@ def real_path(self):
2728
return finders.find(self.path)
2829

2930
def url(self):
30-
return storage.staticfiles_storage.url(self.path)
31+
return self._url
3132

3233

3334
# This will record and map the StaticFile instances with its associated
@@ -58,6 +59,7 @@ def _setup(self):
5859

5960
class DebugStaticFilesStorage(configured_storage_cls):
6061
def url(self, path):
62+
url = super().url(path)
6163
with contextlib.suppress(LookupError):
6264
# For LookupError:
6365
# The ContextVar wasn't set yet. Since the toolbar wasn't properly
@@ -66,10 +68,10 @@ def url(self, path):
6668
request_id = request_id_context_var.get()
6769
record_static_file_signal.send(
6870
sender=self,
69-
staticfile=StaticFile(path),
71+
staticfile=StaticFile(path=str(path), url=url),
7072
request_id=request_id,
7173
)
72-
return super().url(path)
74+
return url
7375

7476
self._wrapped = DebugStaticFilesStorage()
7577

docs/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Pending
77
* Added Python 3.13 to the CI matrix.
88
* Removed support for Python 3.8 as it has reached end of life.
99
* Converted to Django Commons PyPI release process.
10+
* Fixed a crash which occurred when using non-``str`` static file values.
1011

1112
5.0.0-alpha (2024-09-01)
1213
------------------------

tests/panels/test_staticfiles.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from pathlib import Path
2+
13
from django.conf import settings
24
from django.contrib.staticfiles import finders
35
from django.shortcuts import render
4-
from django.test import AsyncRequestFactory
6+
from django.test import AsyncRequestFactory, RequestFactory
57

68
from ..base import BaseTestCase
79

@@ -58,3 +60,19 @@ def test_insert_content(self):
5860
"django.contrib.staticfiles.finders.AppDirectoriesFinder", content
5961
)
6062
self.assertValidHTML(content)
63+
64+
def test_path(self):
65+
def get_response(request):
66+
# template contains one static file
67+
return render(
68+
request,
69+
"staticfiles/path.html",
70+
{"path": Path("additional_static/base.css")},
71+
)
72+
73+
self._get_response = get_response
74+
request = RequestFactory().get("/")
75+
response = self.panel.process_request(request)
76+
self.panel.generate_stats(self.request, response)
77+
self.assertEqual(self.panel.num_used, 1)
78+
self.assertIn('"/static/additional_static/base.css"', self.panel.content)

tests/templates/staticfiles/path.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% load static %}{% static path %}

0 commit comments

Comments
 (0)