From 4806c42efac790dd65bc890b85904df7bdeb1309 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 9 Dec 2024 15:40:47 +0000 Subject: [PATCH] Fixed #35987 -- Made ErrorList.copy() copy the renderer attribute. --- django/forms/utils.py | 1 + tests/forms_tests/tests/test_forms.py | 6 ++++++ tests/forms_tests/tests/test_utils.py | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/django/forms/utils.py b/django/forms/utils.py index d24711d1a0..27eabe57dc 100644 --- a/django/forms/utils.py +++ b/django/forms/utils.py @@ -163,6 +163,7 @@ class ErrorList(UserList, list, RenderableErrorMixin): def copy(self): copy = super().copy() copy.error_class = self.error_class + copy.renderer = self.renderer return copy def get_json_data(self, escape_html=False): diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index d88ac33f24..b49f0a8a69 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -4849,6 +4849,12 @@ class RendererTests(SimpleTestCase): form = CustomForm(renderer=custom) self.assertEqual(form.renderer, custom) + def test_get_context_errors(self): + custom = CustomRenderer() + form = Form(renderer=custom) + context = form.get_context() + self.assertEqual(context["errors"].renderer, custom) + class TemplateTests(SimpleTestCase): def test_iterate_radios(self): diff --git a/tests/forms_tests/tests/test_utils.py b/tests/forms_tests/tests/test_utils.py index f9a5d4c82a..85f7094212 100644 --- a/tests/forms_tests/tests/test_utils.py +++ b/tests/forms_tests/tests/test_utils.py @@ -2,6 +2,7 @@ import copy import json from django.core.exceptions import ValidationError +from django.forms.renderers import DjangoTemplates from django.forms.utils import ( ErrorDict, ErrorList, @@ -161,6 +162,17 @@ class FormsUtilsTestCase(SimpleTestCase): 'example', ) + def test_error_list_copy_attributes(self): + class CustomRenderer(DjangoTemplates): + pass + + renderer = CustomRenderer() + e = ErrorList(error_class="woopsies", renderer=renderer) + + e_copy = e.copy() + self.assertEqual(e.error_class, e_copy.error_class) + self.assertEqual(e.renderer, e_copy.renderer) + def test_error_dict_copy(self): e = ErrorDict() e["__all__"] = ErrorList(