Skip to content

Commit

Permalink
Refs #31026 -- Simplified BaseForm.get_context().
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smithdc1 authored Nov 20, 2023
1 parent ecfea05 commit f1697ec
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions django/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,16 @@ def get_context(self):
hidden_fields = []
top_errors = self.non_field_errors().copy()
for name, bf in self._bound_items():
bf_errors = self.error_class(bf.errors, renderer=self.renderer)
if bf.is_hidden:
if bf_errors:
if bf.errors:
top_errors += [
_("(Hidden field %(name)s) %(error)s")
% {"name": name, "error": str(e)}
for e in bf_errors
for e in bf.errors
]
hidden_fields.append(bf)
else:
errors_str = str(bf_errors)
fields.append((bf, errors_str))
fields.append((bf, bf.errors))
return {
"form": self,
"fields": fields,
Expand Down

0 comments on commit f1697ec

Please sign in to comment.