aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAymeric Augustin2013-10-18 18:08:17 +0200
committerAymeric Augustin2013-10-18 18:09:15 +0200
commitccbf178a3351106e25422b898f09cdca60a1c6e5 (patch)
tree442eb02929f8b2ed640a7d864242c4ee81f4b3cd
parent7196171fadb3478b660451bed344bf13c270f9af (diff)
downloaddjango-debug-toolbar-ccbf178a3351106e25422b898f09cdca60a1c6e5.tar.bz2
Fix crash with objects having a non-ASCII repr in context.
-rw-r--r--debug_toolbar/panels/template.py3
-rw-r--r--tests/templates/basic.html7
-rw-r--r--tests/tests.py4
-rw-r--r--tests/urls.py1
-rw-r--r--tests/views.py13
5 files changed, 24 insertions, 4 deletions
diff --git a/debug_toolbar/panels/template.py b/debug_toolbar/panels/template.py
index 8ecf5d6..f2f813c 100644
--- a/debug_toolbar/panels/template.py
+++ b/debug_toolbar/panels/template.py
@@ -7,6 +7,7 @@ from django import http
from django.conf import settings
from django.template.context import get_standard_processors
from django.test.signals import template_rendered
+from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from django.db.models.query import QuerySet, RawQuerySet
from debug_toolbar.panels import DebugPanel
@@ -92,7 +93,7 @@ class TemplateDebugPanel(DebugPanel):
context_list.append(pformat(temp_layer))
except UnicodeEncodeError:
pass
- kwargs['context'] = context_list
+ kwargs['context'] = [force_text(item) for item in context_list]
self.templates.append(kwargs)
def nav_title(self):
diff --git a/tests/templates/basic.html b/tests/templates/basic.html
new file mode 100644
index 0000000..a0ce0d4
--- /dev/null
+++ b/tests/templates/basic.html
@@ -0,0 +1,7 @@
+<html>
+ <head>
+ <title>{{ title }}</title>
+ </head>
+ <body>
+ </body>
+</html>
diff --git a/tests/tests.py b/tests/tests.py
index 5886c12..86622fc 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -192,6 +192,10 @@ class DebugToolbarIntegrationTestCase(TestCase):
if not six.PY3:
self.assertContains(response, 'là')
+ def test_object_with_non_ascii_repr_in_context(self):
+ response = self.client.get('/non_ascii_context/')
+ self.assertContains(response, 'nôt åscíì')
+
def test_xml_validation(self):
response = self.client.get('/regular/XML/')
ET.fromstring(response.content) # shouldn't raise ParseError
diff --git a/tests/urls.py b/tests/urls.py
index 2877e73..d9ecb8c 100644
--- a/tests/urls.py
+++ b/tests/urls.py
@@ -18,5 +18,6 @@ urlpatterns = patterns('tests.views',
url(r'^resolving2/(?P<arg1>.+)/(?P<arg2>.+)/$', 'resolving_view'),
url(r'^resolving3/(.+)/$', 'resolving_view', { 'arg2' : 'default' }),
url(r'^regular/(?P<title>.*)/$', 'regular_view'),
+ url(r'^non_ascii_context/$', 'non_ascii_context'),
url(r'^execute_sql/$', 'execute_sql'),
)
diff --git a/tests/views.py b/tests/views.py
index 8b51706..5bafc97 100644
--- a/tests/views.py
+++ b/tests/views.py
@@ -4,6 +4,7 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.http import HttpResponse
+from django.shortcuts import render
from django.utils import six
@@ -12,9 +13,15 @@ def execute_sql(request):
return HttpResponse()
+def non_ascii_context(request):
+ class NonAsciiRepr(object):
+ def __repr__(self):
+ return 'nôt åscíì' if six.PY3 else 'nôt åscíì'.encode('utf-8')
+ return render(request, 'basic.html', {'title': NonAsciiRepr()})
+
+
def regular_view(request, title):
- content = '<html><head><title>%s</title></head><body></body></html>' % title
- return HttpResponse(content)
+ return render(request, 'basic.html', {'title': title})
def resolving_view(request, arg1, arg2):
@@ -26,4 +33,4 @@ def set_session(request):
request.session['où'] = 'où'
if not six.PY3:
request.session['là'.encode('utf-8')] = 'là'.encode('utf-8')
- return HttpResponse('<html><head></head><body></body></html>')
+ return render(request, 'basic.html')