Skip to content

Commit ccbf178

Browse files
committed
Fix crash with objects having a non-ASCII repr in context.
1 parent 7196171 commit ccbf178

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

debug_toolbar/panels/template.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.conf import settings
88
from django.template.context import get_standard_processors
99
from django.test.signals import template_rendered
10+
from django.utils.encoding import force_text
1011
from django.utils.translation import ugettext_lazy as _
1112
from django.db.models.query import QuerySet, RawQuerySet
1213
from debug_toolbar.panels import DebugPanel
@@ -92,7 +93,7 @@ def _store_template_info(self, sender, **kwargs):
9293
context_list.append(pformat(temp_layer))
9394
except UnicodeEncodeError:
9495
pass
95-
kwargs['context'] = context_list
96+
kwargs['context'] = [force_text(item) for item in context_list]
9697
self.templates.append(kwargs)
9798

9899
def nav_title(self):

tests/templates/basic.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head>
3+
<title>{{ title }}</title>
4+
</head>
5+
<body>
6+
</body>
7+
</html>

tests/tests.py

+4
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ def test_non_ascii_session(self):
192192
if not six.PY3:
193193
self.assertContains(response, 'là')
194194

195+
def test_object_with_non_ascii_repr_in_context(self):
196+
response = self.client.get('/non_ascii_context/')
197+
self.assertContains(response, 'nôt åscíì')
198+
195199
def test_xml_validation(self):
196200
response = self.client.get('/regular/XML/')
197201
ET.fromstring(response.content) # shouldn't raise ParseError

tests/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
url(r'^resolving2/(?P<arg1>.+)/(?P<arg2>.+)/$', 'resolving_view'),
1919
url(r'^resolving3/(.+)/$', 'resolving_view', { 'arg2' : 'default' }),
2020
url(r'^regular/(?P<title>.*)/$', 'regular_view'),
21+
url(r'^non_ascii_context/$', 'non_ascii_context'),
2122
url(r'^execute_sql/$', 'execute_sql'),
2223
)

tests/views.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from django.contrib.auth.models import User
66
from django.http import HttpResponse
7+
from django.shortcuts import render
78
from django.utils import six
89

910

@@ -12,9 +13,15 @@ def execute_sql(request):
1213
return HttpResponse()
1314

1415

16+
def non_ascii_context(request):
17+
class NonAsciiRepr(object):
18+
def __repr__(self):
19+
return 'nôt åscíì' if six.PY3 else 'nôt åscíì'.encode('utf-8')
20+
return render(request, 'basic.html', {'title': NonAsciiRepr()})
21+
22+
1523
def regular_view(request, title):
16-
content = '<html><head><title>%s</title></head><body></body></html>' % title
17-
return HttpResponse(content)
24+
return render(request, 'basic.html', {'title': title})
1825

1926

2027
def resolving_view(request, arg1, arg2):
@@ -26,4 +33,4 @@ def set_session(request):
2633
request.session['où'] = 'où'
2734
if not six.PY3:
2835
request.session['là'.encode('utf-8')] = 'là'.encode('utf-8')
29-
return HttpResponse('<html><head></head><body></body></html>')
36+
return render(request, 'basic.html')

0 commit comments

Comments
 (0)