Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17529 -- get_template_from_string default arguments break

``get_template_from_string`` default arguments were breaking
``assertTemplateUsed``. The solution has been to return only the names of the
templates with a ``name`` attribute distinct of ``None``. The default ``name``
kwarg of ``Template`` has been changed to ``None``, more pythonic than ``'<Unknown
Template>'``.
  • Loading branch information...
commit 72f63bd24d44b5a4f24ad8fa27ebba96d9a507d8 1 parent bc21e9c
Unai Zalakain authored November 04, 2013 claudep committed November 08, 2013
3  django/template/base.py
@@ -121,8 +121,7 @@ def reload(self):
121 121
 
122 122
 
123 123
 class Template(object):
124  
-    def __init__(self, template_string, origin=None,
125  
-                 name='<Unknown Template>'):
  124
+    def __init__(self, template_string, origin=None, name=None):
126 125
         try:
127 126
             template_string = force_text(template_string)
128 127
         except UnicodeDecodeError:
3  django/test/testcases.py
@@ -506,7 +506,8 @@ def _assert_template_used(self, response, template_name, msg_prefix):
506 506
             # use this template with context manager
507 507
             return template_name, None, msg_prefix
508 508
 
509  
-        template_names = [t.name for t in response.templates]
  509
+        template_names = [t.name for t in response.templates if t.name is not
  510
+                          None]
510 511
         return None, template_names, msg_prefix
511 512
 
512 513
     def assertTemplateUsed(self, response=None, template_name=None, msg_prefix=''):
7  tests/test_utils/tests.py
@@ -214,6 +214,8 @@ def test_with_client(self):
214 214
 
215 215
 
216 216
 class AssertTemplateUsedContextManagerTests(TestCase):
  217
+    urls = 'test_utils.urls'
  218
+
217 219
     def test_usage(self):
218 220
         with self.assertTemplateUsed('template_used/base.html'):
219 221
             render_to_string('template_used/base.html')
@@ -270,6 +272,11 @@ def test_error_message(self):
270 272
             with self.assertTemplateUsed('template_used/base.html'):
271 273
                 render_to_string('template_used/alternative.html')
272 274
 
  275
+        with self.assertRaises(AssertionError) as cm:
  276
+            response = self.client.get('/test_utils/no_template_used/')
  277
+            self.assertTemplateUsed(response, 'template_used/base.html')
  278
+        self.assertEqual(cm.exception.args[0], "No templates used to render the response")
  279
+
273 280
     def test_failure(self):
274 281
         with self.assertRaises(TypeError):
275 282
             with self.assertTemplateUsed():
1  tests/test_utils/urls.py
@@ -5,4 +5,5 @@
5 5
 
6 6
 urlpatterns = patterns('',
7 7
     (r'^test_utils/get_person/(\d+)/$', views.get_person),
  8
+    (r'^test_utils/no_template_used/$', views.no_template_used),
8 9
 )
5  tests/test_utils/views.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django.http import HttpResponse
2 2
 from django.shortcuts import get_object_or_404
  3
+from django.template import loader, Context
3 4
 
4 5
 from .models import Person
5 6
 
@@ -7,3 +8,7 @@
7 8
 def get_person(request, pk):
8 9
     person = get_object_or_404(Person, pk=pk)
9 10
     return HttpResponse(person.name)
  11
+
  12
+def no_template_used(request):
  13
+    template = loader.get_template_from_string("This is a string-based template")
  14
+    return HttpResponse(template.render(Context({})))

0 notes on commit 72f63bd

Please sign in to comment.
Something went wrong with that request. Please try again.