1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Refs #31026 -- Removed ability to return string when rendering ErrorDict/ErrorList.

Per deprecation timeline.
This commit is contained in:
Mariusz Felisiak 2023-01-11 06:07:16 +01:00
parent 182d25eb7a
commit 31878b4d73
4 changed files with 3 additions and 72 deletions

View File

@ -4,16 +4,13 @@ Form classes
import copy import copy
import datetime import datetime
import warnings
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
from django.forms.fields import Field, FileField from django.forms.fields import Field, FileField
from django.forms.utils import ErrorDict, ErrorList, RenderableFormMixin from django.forms.utils import ErrorDict, ErrorList, RenderableFormMixin
from django.forms.widgets import Media, MediaDefiningClass from django.forms.widgets import Media, MediaDefiningClass
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.safestring import SafeString, mark_safe
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from .renderers import get_default_renderer from .renderers import get_default_renderer
@ -238,15 +235,6 @@ class BaseForm(RenderableFormMixin):
hidden_fields.append(bf) hidden_fields.append(bf)
else: else:
errors_str = str(bf_errors) errors_str = str(bf_errors)
# RemovedInDjango50Warning.
if not isinstance(errors_str, SafeString):
warnings.warn(
f"Returning a plain string from "
f"{self.error_class.__name__} is deprecated. Please "
f"customize via the template system instead.",
RemovedInDjango50Warning,
)
errors_str = mark_safe(errors_str)
fields.append((bf, errors_str)) fields.append((bf, errors_str))
return { return {
"form": self, "form": self,

View File

@ -1015,11 +1015,6 @@ Customizing the error list format
overriding the default template, see also overriding the default template, see also
:ref:`overriding-built-in-form-templates`. :ref:`overriding-built-in-form-templates`.
.. deprecated:: 4.0
The ability to return a ``str`` when calling the ``__str__`` method is
deprecated. Use the template engine instead which returns a ``SafeString``.
More granular output More granular output
==================== ====================

View File

@ -299,6 +299,9 @@ to remove usage of these features.
* The undocumented ``BaseForm._html_output()`` method is removed. * The undocumented ``BaseForm._html_output()`` method is removed.
* The ability to return a ``str``, rather than a ``SafeString``, when rendering
an ``ErrorDict`` and ``ErrorList`` is removed.
See :ref:`deprecated-features-4.1` for details on these changes, including how See :ref:`deprecated-features-4.1` for details on these changes, including how
to remove usage of these features. to remove usage of these features.

View File

@ -1,55 +0,0 @@
# RemovedInDjango50
from django.forms import CharField, EmailField, Form
from django.forms.utils import ErrorList
from django.test import SimpleTestCase, ignore_warnings
from django.utils.deprecation import RemovedInDjango50Warning
class DivErrorList(ErrorList):
def __str__(self):
return self.as_divs()
def as_divs(self):
if not self:
return ""
return '<div class="errorlist">%s</div>' % "".join(
f'<div class="error">{error}</div>' for error in self
)
class DeprecationTests(SimpleTestCase):
def test_deprecation_warning_error_list(self):
class EmailForm(Form):
email = EmailField()
comment = CharField()
data = {"email": "invalid"}
f = EmailForm(data, error_class=DivErrorList)
msg = (
"Returning a plain string from DivErrorList is deprecated. Please "
"customize via the template system instead."
)
with self.assertRaisesMessage(RemovedInDjango50Warning, msg):
f.as_p()
@ignore_warnings(category=RemovedInDjango50Warning)
class DeprecatedTests(SimpleTestCase):
def test_errorlist_override_str(self):
class CommentForm(Form):
name = CharField(max_length=50, required=False)
email = EmailField()
comment = CharField()
data = {"email": "invalid"}
f = CommentForm(data, auto_id=False, error_class=DivErrorList)
self.assertHTMLEqual(
f.as_p(),
'<p>Name: <input type="text" name="name" maxlength="50"></p>'
'<div class="errorlist">'
'<div class="error">Enter a valid email address.</div></div>'
'<p>Email: <input type="email" name="email" value="invalid" required></p>'
'<div class="errorlist">'
'<div class="error">This field is required.</div></div>'
'<p>Comment: <input type="text" name="comment" required></p>',
)