Fixed #35987 -- Made ErrorList.copy() copy the renderer attribute.

This commit is contained in:
Adam Johnson 2024-12-09 15:40:47 +00:00 committed by Sarah Boyce
parent 02628c051c
commit 4806c42efa
3 changed files with 19 additions and 0 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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):
'<a href="http://www.example.com/">example</a></li></ul>',
)
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(