File tree Expand file tree Collapse file tree 9 files changed +40
-9
lines changed Expand file tree Collapse file tree 9 files changed +40
-9
lines changed Original file line number Diff line number Diff line change 33from django import forms
44from django .core import signing
55from django .core .exceptions import ValidationError
6- from django .utils .encoding import force_str
6+
7+ from debug_toolbar .sanitize import force_str
78
89
910class SignedDataForm (forms .Form ):
Original file line number Diff line number Diff line change 1- from django .utils .encoding import force_str
21from django .utils .translation import gettext_lazy as _
32from django .views .debug import get_default_exception_reporter_filter
43
54from debug_toolbar .panels import Panel
5+ from debug_toolbar .sanitize import force_str
66
77get_safe_settings = get_default_exception_reporter_filter ().get_safe_settings
88
Original file line number Diff line number Diff line change 55from time import perf_counter
66
77import django .test .testcases
8- from django .utils .encoding import force_str
98
9+ from debug_toolbar .sanitize import force_str
1010from debug_toolbar .utils import get_stack_trace , get_template_info
1111
1212try :
@@ -128,10 +128,7 @@ def _decode(self, param):
128128
129129 # make sure datetime, date and time are converted to string by force_str
130130 CONVERT_TYPES = (datetime .datetime , datetime .date , datetime .time )
131- try :
132- return force_str (param , strings_only = not isinstance (param , CONVERT_TYPES ))
133- except UnicodeDecodeError :
134- return "(encoded string)"
131+ return force_str (param , strings_only = not isinstance (param , CONVERT_TYPES ))
135132
136133 def _last_executed_query (self , sql , params ):
137134 """Get the last executed query from the connection."""
Original file line number Diff line number Diff line change 1010from django .test .signals import template_rendered
1111from django .test .utils import instrumented_test_render
1212from django .urls import path
13- from django .utils .encoding import force_str
1413from django .utils .translation import gettext_lazy as _
1514
1615from debug_toolbar .panels import Panel
1716from debug_toolbar .panels .sql .tracking import SQLQueryTriggered , allow_sql
1817from debug_toolbar .panels .templates import views
18+ from debug_toolbar .sanitize import force_str
1919
2020if find_spec ("jinja2" ):
2121 from debug_toolbar .panels .templates .jinja2 import patch_jinja_render
Original file line number Diff line number Diff line change 1+ from django .utils .encoding import DjangoUnicodeDecodeError , force_str as force_string
2+
3+
4+ def force_str (s , * args , ** kwargs ):
5+ """
6+ Forces values to strings.
7+ Will return "Django Debug Toolbar was unable to parse value." when there's a decoding error.
8+ """
9+ try :
10+ return force_string (s , * args , ** kwargs )
11+ except DjangoUnicodeDecodeError :
12+ return "Django Debug Toolbar was unable to parse value."
Original file line number Diff line number Diff line change 66
77from django .core .serializers .json import DjangoJSONEncoder
88from django .db import transaction
9- from django .utils .encoding import force_str
109from django .utils .module_loading import import_string
1110
1211from debug_toolbar import settings as dt_settings
1312from debug_toolbar .models import HistoryEntry
13+ from debug_toolbar .sanitize import force_str
1414
1515
1616class DebugToolbarJSONEncoder (DjangoJSONEncoder ):
Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ Pending
1616* Upgraded CI ``postgis `` version to 17-3.5.
1717* Added how to generate the documentation locally to the contributing
1818 documentation.
19+ * Updated logic that forces values to strings (``force_str ``) to render
20+ "Django Debug Toolbar was unable to parse value." when there's a decoding
21+ error.
1922
20236.0.0 (2025-07-22)
2124------------------
Original file line number Diff line number Diff line change 1+ import unittest
2+
3+ from debug_toolbar .sanitize import force_str
4+
5+
6+ class ForceStrTestCase (unittest .TestCase ):
7+ def test_success_convert (self ):
8+ input = 0
9+
10+ self .assertEqual (force_str (input ), "0" )
11+
12+ def test_failed_convert (self ):
13+ input = bytes .fromhex (
14+ "a3f2b8c14e972d5a8fb3c7291a64e0859c472bf63d18a0945e73b2c84f917ae2"
15+ )
16+ self .assertEqual (
17+ force_str (input ), "Django Debug Toolbar was unable to parse value."
18+ )
You can’t perform that action at this time.
0 commit comments