A collection of errors that knows how to display itself in various formats.
| 139 | |
| 140 | |
| 141 | class ErrorList(UserList, list, RenderableErrorMixin): |
| 142 | """ |
| 143 | A collection of errors that knows how to display itself in various formats. |
| 144 | """ |
| 145 | |
| 146 | template_name = "django/forms/errors/list/default.html" |
| 147 | template_name_text = "django/forms/errors/list/text.txt" |
| 148 | template_name_ul = "django/forms/errors/list/ul.html" |
| 149 | |
| 150 | def __init__(self, initlist=None, error_class=None, renderer=None, field_id=None): |
| 151 | super().__init__(initlist) |
| 152 | |
| 153 | if error_class is None: |
| 154 | self.error_class = "errorlist" |
| 155 | else: |
| 156 | self.error_class = "errorlist {}".format(error_class) |
| 157 | self.renderer = renderer or get_default_renderer() |
| 158 | self.field_id = field_id |
| 159 | |
| 160 | def as_data(self): |
| 161 | return ValidationError(self.data).error_list |
| 162 | |
| 163 | def copy(self): |
| 164 | copy = super().copy() |
| 165 | copy.error_class = self.error_class |
| 166 | copy.renderer = self.renderer |
| 167 | return copy |
| 168 | |
| 169 | def get_json_data(self, escape_html=False): |
| 170 | errors = [] |
| 171 | for error in self.as_data(): |
| 172 | message = next(iter(error)) |
| 173 | errors.append( |
| 174 | { |
| 175 | "message": escape(message) if escape_html else message, |
| 176 | "code": error.code or "", |
| 177 | } |
| 178 | ) |
| 179 | return errors |
| 180 | |
| 181 | def get_context(self): |
| 182 | return { |
| 183 | "errors": self, |
| 184 | "error_class": self.error_class, |
| 185 | } |
| 186 | |
| 187 | def __repr__(self): |
| 188 | return repr(list(self)) |
| 189 | |
| 190 | def __contains__(self, item): |
| 191 | return item in list(self) |
| 192 | |
| 193 | def __eq__(self, other): |
| 194 | return list(self) == other |
| 195 | |
| 196 | def __getitem__(self, i): |
| 197 | error = self.data[i] |
| 198 | if isinstance(error, ValidationError): |
no outgoing calls