Refs #32339 -- Changed default form and formset rendering style to div-based.

Per deprecation timeline.

This also removes "django/forms/default.html" and
"django/forms/formsets/default.html" templates.
This commit is contained in:
Mariusz Felisiak 2023-01-13 09:32:35 +01:00
parent b5ac6e78f8
commit 98756c685e
13 changed files with 12 additions and 96 deletions

View File

@ -32,8 +32,6 @@ class ManagementForm(Form):
as well.
"""
template_name = "django/forms/div.html" # RemovedInDjango50Warning.
TOTAL_FORMS = IntegerField(widget=HiddenInput)
INITIAL_FORMS = IntegerField(widget=HiddenInput)
# MIN_NUM_FORM_COUNT and MAX_NUM_FORM_COUNT are output with the rest of the

View File

@ -1 +0,0 @@
{% include "django/forms/table.html" %}

View File

@ -1 +0,0 @@
{{ formset.management_form }}{% for form in formset %}{{ form }}{% endfor %}

View File

@ -15,11 +15,8 @@ def get_default_renderer():
class BaseRenderer:
# RemovedInDjango50Warning: When the deprecation ends, replace with
# form_template_name = "django/forms/div.html"
# formset_template_name = "django/forms/formsets/div.html"
form_template_name = "django/forms/default.html"
formset_template_name = "django/forms/formsets/default.html"
form_template_name = "django/forms/div.html"
formset_template_name = "django/forms/formsets/div.html"
def get_template(self, template_name):
raise NotImplementedError("subclasses must implement get_template()")

View File

@ -1 +0,0 @@
{% include "django/forms/table.html" %}

View File

@ -1 +0,0 @@
{{ formset.management_form }}{% for form in formset %}{{ form }}{% endfor %}

View File

@ -1,16 +1,13 @@
import json
import warnings
from collections import UserList
from django.conf import settings
from django.core.exceptions import ValidationError
from django.forms.renderers import get_default_renderer
from django.utils import timezone
from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.html import escape, format_html_join
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.utils.version import get_docs_version
def pretty_name(name):
@ -45,16 +42,6 @@ def flatatt(attrs):
)
DEFAULT_TEMPLATE_DEPRECATION_MSG = (
'The "default.html" templates for forms and formsets will be removed. These were '
'proxies to the equivalent "table.html" templates, but the new "div.html" '
"templates will be the default from Django 5.0. Transitional renderers are "
"provided to allow you to opt-in to the new output style now. See "
"https://docs.djangoproject.com/en/%s/releases/4.1/ for more details"
% get_docs_version()
)
class RenderableMixin:
def get_context(self):
raise NotImplementedError(
@ -65,13 +52,6 @@ class RenderableMixin:
renderer = renderer or self.renderer
template = template_name or self.template_name
context = context or self.get_context()
if (
template == "django/forms/default.html"
or template == "django/forms/formsets/default.html"
):
warnings.warn(
DEFAULT_TEMPLATE_DEPRECATION_MSG, RemovedInDjango50Warning, stacklevel=2
)
return mark_safe(renderer.render(template, context))
__str__ = render

View File

@ -51,27 +51,13 @@ should return a rendered templates (as a string) or raise
The default name of the template to use to render a form.
Defaults to ``"django/forms/default.html"``, which is a proxy for
``"django/forms/table.html"``.
.. deprecated:: 4.1
The ``"django/forms/default.html"`` template is deprecated and will be
removed in Django 5.0. The default will become
``"django/forms/div.html"`` at that time.
Defaults to ``"django/forms/div.html"`` template.
.. attribute:: formset_template_name
The default name of the template to use to render a formset.
Defaults to ``"django/forms/formsets/default.html"``, which is a proxy
for ``"django/forms/formsets/table.html"``.
.. deprecated:: 4.1
The ``"django/forms/formset/default.html"`` template is deprecated and
will be removed in Django 5.0. The default will become
``"django/forms/formset/div.html"`` template.
Defaults to ``"django/forms/formsets/div.html"`` template.
.. method:: get_template(template_name)

View File

@ -342,3 +342,8 @@ to remove usage of these features.
* The ``django.contrib.gis.admin.OpenLayersWidget`` is removed.
+ The ``django.contrib.auth.hashers.CryptPasswordHasher`` is removed.
* The ``"django/forms/default.html"`` and
``"django/forms/formsets/default.html"`` templates are removed.
* The default form and formset rendering style is changed to the div-based.

View File

@ -11,8 +11,6 @@ except ImportError:
def jinja2_tests(test_func):
test_func = skipIf(jinja2 is None, "this test requires jinja2")(test_func)
return override_settings(
# RemovedInDjango50Warning: When the deprecation ends, revert to
# FORM_RENDERER="django.forms.renderers.Jinja2",
FORM_RENDERER="django.forms.renderers.Jinja2DivFormRenderer",
FORM_RENDERER="django.forms.renderers.Jinja2",
TEMPLATES={"BACKEND": "django.template.backends.jinja2.Jinja2"},
)(test_func)

View File

@ -42,9 +42,8 @@ from django.forms.utils import ErrorList
from django.http import QueryDict
from django.template import Context, Template
from django.test import SimpleTestCase
from django.test.utils import isolate_lru_cache, override_settings
from django.test.utils import override_settings
from django.utils.datastructures import MultiValueDict
from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.safestring import mark_safe
from . import jinja2_tests
@ -5084,16 +5083,3 @@ class OverrideTests(SimpleTestCase):
'<label for="id_name" class="required">Name:</label>'
'<legend class="required">Language:</legend>',
)
class DeprecationTests(SimpleTestCase):
def test_warning(self):
from django.forms.utils import DEFAULT_TEMPLATE_DEPRECATION_MSG
with isolate_lru_cache(get_default_renderer), self.settings(
FORM_RENDERER="django.forms.renderers.DjangoTemplates"
), self.assertRaisesMessage(
RemovedInDjango50Warning, DEFAULT_TEMPLATE_DEPRECATION_MSG
):
form = Person()
str(form)

View File

@ -23,12 +23,10 @@ from django.forms.formsets import (
all_valid,
formset_factory,
)
from django.forms.renderers import TemplatesSetting, get_default_renderer
from django.forms.renderers import TemplatesSetting
from django.forms.utils import ErrorList
from django.forms.widgets import HiddenInput
from django.test import SimpleTestCase
from django.test.utils import isolate_lru_cache
from django.utils.deprecation import RemovedInDjango50Warning
from . import jinja2_tests
@ -1900,28 +1898,3 @@ class AllValidTests(SimpleTestCase):
]
self.assertEqual(formset1._errors, expected_errors)
self.assertEqual(formset2._errors, expected_errors)
class DeprecationTests(SimpleTestCase):
def test_warning(self):
from django.forms.utils import DEFAULT_TEMPLATE_DEPRECATION_MSG
with isolate_lru_cache(get_default_renderer), self.settings(
FORM_RENDERER="django.forms.renderers.DjangoTemplates"
), self.assertRaisesMessage(
RemovedInDjango50Warning, DEFAULT_TEMPLATE_DEPRECATION_MSG
):
ChoiceFormSet = formset_factory(Choice)
formset = ChoiceFormSet()
str(formset)
def test_no_management_form_warning(self):
"""
Management forms are already rendered with the new div template.
"""
with isolate_lru_cache(get_default_renderer), self.settings(
FORM_RENDERER="django.forms.renderers.DjangoTemplates"
):
ChoiceFormSet = formset_factory(Choice, formset=BaseFormSet)
formset = ChoiceFormSet()
str(formset.management_form)

View File

@ -253,9 +253,6 @@ def setup_collect_tests(start_at, start_after, test_labels=None):
"fields.W907",
]
# RemovedInDjango50Warning
settings.FORM_RENDERER = "django.forms.renderers.DjangoDivFormRenderer"
# Load all the ALWAYS_INSTALLED_APPS.
django.setup()