diff --git a/debug_toolbar/panels/settings.py b/debug_toolbar/panels/settings.py index 68ab44c0b..e985e7790 100644 --- a/debug_toolbar/panels/settings.py +++ b/debug_toolbar/panels/settings.py @@ -1,4 +1,4 @@ -from django.utils.encoding import force_str +from django.utils.encoding import DjangoUnicodeDecodeError, force_str from django.utils.translation import gettext_lazy as _ from django.views.debug import get_default_exception_reporter_filter @@ -24,10 +24,16 @@ def title(self): ) def generate_stats(self, request, response): + def catch_force_errors(force_function, value): + try: + return force_function(value) + except DjangoUnicodeDecodeError: + return "Debug toolbar was unable to parse value" + self.record_stats( { "settings": { - key: force_str(value) + key: catch_force_errors(force_str, value) for key, value in sorted(get_safe_settings().items()) } } diff --git a/docs/changes.rst b/docs/changes.rst index 452242279..ae4b89f21 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -13,6 +13,8 @@ Pending class instance, regardless if any data was generated. * Fixed selenium tests for CI by using psycopg for Python 3.13 runs. * Added ``CommunityPanel`` containing links to documentation and resources. +* Fixed force_str to catch error and give out default string if value is not + serializable. 6.0.0 (2025-07-22) ------------------ diff --git a/tests/panels/test_settings.py b/tests/panels/test_settings.py index 89b016dc0..5ea5345a9 100644 --- a/tests/panels/test_settings.py +++ b/tests/panels/test_settings.py @@ -3,7 +3,12 @@ from ..base import IntegrationTestCase -@override_settings(DEBUG=True) +@override_settings( + DEBUG=True, + RANDOM_SETTING=bytes.fromhex( + "a3f2b8c14e972d5a8fb3c7291a64e0859c472bf63d18a0945e73b2c84f917ae2" + ), +) class SettingsIntegrationTestCase(IntegrationTestCase): def test_panel_title(self): response = self.client.get("/regular/basic/")