1
0
mirror of https://github.com/django/django.git synced 2025-01-03 15:06:09 +00:00

Refs #31026 -- Simplified BaseForm.get_context().

bf.errors returns an ErrorList. Access this directly and avoid creating
a new instance in BaseForm.get_context()

Calling str() on the ErrorList can also be deferred to when the
variable used in the template.
This commit is contained in:
David Smith 2023-11-20 07:57:03 +00:00 committed by GitHub
parent ecfea054ee
commit f1697ec7c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -224,18 +224,16 @@ class BaseForm(RenderableFormMixin):
hidden_fields = [] hidden_fields = []
top_errors = self.non_field_errors().copy() top_errors = self.non_field_errors().copy()
for name, bf in self._bound_items(): for name, bf in self._bound_items():
bf_errors = self.error_class(bf.errors, renderer=self.renderer)
if bf.is_hidden: if bf.is_hidden:
if bf_errors: if bf.errors:
top_errors += [ top_errors += [
_("(Hidden field %(name)s) %(error)s") _("(Hidden field %(name)s) %(error)s")
% {"name": name, "error": str(e)} % {"name": name, "error": str(e)}
for e in bf_errors for e in bf.errors
] ]
hidden_fields.append(bf) hidden_fields.append(bf)
else: else:
errors_str = str(bf_errors) fields.append((bf, bf.errors))
fields.append((bf, errors_str))
return { return {
"form": self, "form": self,
"fields": fields, "fields": fields,