mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[5.0.x] Fixed #34823 -- Fixed assertTemplateUsed() context manager crash on unnamed templates.
Backport of 51d703a27f from main
			
			
This commit is contained in:
		| @@ -116,18 +116,16 @@ class _AssertTemplateUsedContext: | |||||||
|         self.count = count |         self.count = count | ||||||
|  |  | ||||||
|         self.rendered_templates = [] |         self.rendered_templates = [] | ||||||
|         self.rendered_template_names = [] |  | ||||||
|         self.context = ContextList() |         self.context = ContextList() | ||||||
|  |  | ||||||
|     def on_template_render(self, sender, signal, template, context, **kwargs): |     def on_template_render(self, sender, signal, template, context, **kwargs): | ||||||
|         self.rendered_templates.append(template) |         self.rendered_templates.append(template) | ||||||
|         self.rendered_template_names.append(template.name) |  | ||||||
|         self.context.append(copy(context)) |         self.context.append(copy(context)) | ||||||
|  |  | ||||||
|     def test(self): |     def test(self): | ||||||
|         self.test_case._assert_template_used( |         self.test_case._assert_template_used( | ||||||
|             self.template_name, |             self.template_name, | ||||||
|             self.rendered_template_names, |             [t.name for t in self.rendered_templates if t.name is not None], | ||||||
|             self.msg_prefix, |             self.msg_prefix, | ||||||
|             self.count, |             self.count, | ||||||
|         ) |         ) | ||||||
| @@ -145,8 +143,11 @@ class _AssertTemplateUsedContext: | |||||||
|  |  | ||||||
| class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext): | class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext): | ||||||
|     def test(self): |     def test(self): | ||||||
|  |         rendered_template_names = [ | ||||||
|  |             t.name for t in self.rendered_templates if t.name is not None | ||||||
|  |         ] | ||||||
|         self.test_case.assertFalse( |         self.test_case.assertFalse( | ||||||
|             self.template_name in self.rendered_template_names, |             self.template_name in rendered_template_names, | ||||||
|             f"{self.msg_prefix}Template '{self.template_name}' was used " |             f"{self.msg_prefix}Template '{self.template_name}' was used " | ||||||
|             f"unexpectedly in rendering the response", |             f"unexpectedly in rendering the response", | ||||||
|         ) |         ) | ||||||
|   | |||||||
| @@ -26,6 +26,7 @@ from django.forms import ( | |||||||
|     formset_factory, |     formset_factory, | ||||||
| ) | ) | ||||||
| from django.http import HttpResponse | from django.http import HttpResponse | ||||||
|  | from django.template import Context, Template | ||||||
| from django.template.loader import render_to_string | from django.template.loader import render_to_string | ||||||
| from django.test import ( | from django.test import ( | ||||||
|     SimpleTestCase, |     SimpleTestCase, | ||||||
| @@ -541,12 +542,20 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase): | |||||||
|             with self.assertTemplateUsed("template_used/base.html"): |             with self.assertTemplateUsed("template_used/base.html"): | ||||||
|                 render_to_string("template_used/alternative.html") |                 render_to_string("template_used/alternative.html") | ||||||
|  |  | ||||||
|         with self.assertRaisesMessage( |         msg = "No templates used to render the response" | ||||||
|             AssertionError, "No templates used to render the response" |         with self.assertRaisesMessage(AssertionError, msg): | ||||||
|         ): |  | ||||||
|             response = self.client.get("/test_utils/no_template_used/") |             response = self.client.get("/test_utils/no_template_used/") | ||||||
|             self.assertTemplateUsed(response, "template_used/base.html") |             self.assertTemplateUsed(response, "template_used/base.html") | ||||||
|  |  | ||||||
|  |         with self.assertRaisesMessage(AssertionError, msg): | ||||||
|  |             with self.assertTemplateUsed("template_used/base.html"): | ||||||
|  |                 self.client.get("/test_utils/no_template_used/") | ||||||
|  |  | ||||||
|  |         with self.assertRaisesMessage(AssertionError, msg): | ||||||
|  |             with self.assertTemplateUsed("template_used/base.html"): | ||||||
|  |                 template = Template("template_used/alternative.html", name=None) | ||||||
|  |                 template.render(Context()) | ||||||
|  |  | ||||||
|     def test_msg_prefix(self): |     def test_msg_prefix(self): | ||||||
|         msg_prefix = "Prefix" |         msg_prefix = "Prefix" | ||||||
|         msg = f"{msg_prefix}: No templates used to render the response" |         msg = f"{msg_prefix}: No templates used to render the response" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user