Refs #32339 -- Deprecated transitional form renderers.

This commit is contained in:
Mariusz Felisiak 2023-01-18 11:08:39 +01:00 committed by GitHub
parent 3bbe22dafc
commit b209518089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 31 deletions

View File

@ -1,9 +1,11 @@
import functools
import warnings
from pathlib import Path
from django.conf import settings
from django.template.backends.django import DjangoTemplates
from django.template.loader import get_template
from django.utils.deprecation import RemovedInDjango60Warning
from django.utils.functional import cached_property
from django.utils.module_loading import import_string
@ -64,6 +66,7 @@ class Jinja2(EngineMixin, BaseRenderer):
return Jinja2
# RemovedInDjango60Warning.
class DjangoDivFormRenderer(DjangoTemplates):
"""
Load Django templates from django/forms/templates and from apps'
@ -71,22 +74,29 @@ class DjangoDivFormRenderer(DjangoTemplates):
formsets.
"""
# RemovedInDjango50Warning Deprecate this class in 5.0 and remove in 6.0.
form_template_name = "django/forms/div.html"
formset_template_name = "django/forms/formsets/div.html"
def __init__(self, *args, **kwargs):
warnings.warn(
"The DjangoDivFormRenderer transitional form renderer is deprecated. Use "
"DjangoTemplates instead.",
RemovedInDjango60Warning,
)
super.__init__(*args, **kwargs)
# RemovedInDjango60Warning.
class Jinja2DivFormRenderer(Jinja2):
"""
Load Jinja2 templates from the built-in widget templates in
django/forms/jinja2 and from apps' 'jinja2' directory.
"""
# RemovedInDjango50Warning Deprecate this class in 5.0 and remove in 6.0.
form_template_name = "django/forms/div.html"
formset_template_name = "django/forms/formsets/div.html"
def __init__(self, *args, **kwargs):
warnings.warn(
"The Jinja2DivFormRenderer transitional form renderer is deprecated. Use "
"Jinja2 instead.",
RemovedInDjango60Warning,
)
super.__init__(*args, **kwargs)
class TemplatesSetting(BaseRenderer):

View File

@ -15,6 +15,9 @@ about each item can often be found in the release notes of two versions prior.
See the :ref:`Django 5.0 release notes <deprecated-features-5.0>` for more
details on these changes.
* The ``DjangoDivFormRenderer`` and ``Jinja2DivFormRenderer`` transitional form
renderers will be removed.
.. _deprecation-removed-in-5.1:
5.1

View File

@ -93,21 +93,9 @@ If you want to render templates with customizations from your
.. class:: DjangoDivFormRenderer
Subclass of :class:`DjangoTemplates` that specifies
:attr:`~BaseRenderer.form_template_name` and
:attr:`~BaseRenderer.formset_template_name` as ``"django/forms/div.html"`` and
``"django/forms/formset/div.html"`` respectively.
.. deprecated:: 5.0
This is a transitional renderer for opt-in to the new ``<div>`` based
templates, which are the default from Django 5.0.
Apply this via the :setting:`FORM_RENDERER` setting::
FORM_RENDERER = "django.forms.renderers.DjangoDivFormRenderer"
Once the ``<div>`` templates are the default, this transitional renderer will
be deprecated, for removal in Django 6.0. The ``FORM_RENDERER`` declaration can
be removed at that time.
The alias of :class:`DjangoTemplates`.
``Jinja2``
----------
@ -127,12 +115,9 @@ widgets due to their usage of Django template tags.
.. class:: Jinja2DivFormRenderer
A transitional renderer as per :class:`DjangoDivFormRenderer` above, but
subclassing :class:`Jinja2` for use with the Jinja2 backend.
.. deprecated:: 5.0
Apply this via the :setting:`FORM_RENDERER` setting::
FORM_RENDERER = "django.forms.renderers.Jinja2DivFormRenderer"
The alias of :class:`Jinja2`.
``TemplatesSetting``
--------------------

View File

@ -233,9 +233,9 @@ Forms
* In order to smooth adoption of the new ``<div>`` output style, two
transitional form renderer classes are available:
:class:`django.forms.renderers.DjangoDivFormRenderer` and
:class:`django.forms.renderers.Jinja2DivFormRenderer`, for the Django and
Jinja2 template backends respectively.
``django.forms.renderers.DjangoDivFormRenderer`` and
``django.forms.renderers.Jinja2DivFormRenderer``, for the Django and Jinja2
template backends respectively.
You can apply one of these via the :setting:`FORM_RENDERER` setting. For
example::

View File

@ -247,7 +247,8 @@ Features deprecated in 5.0
Miscellaneous
-------------
* ...
* The ``DjangoDivFormRenderer`` and ``Jinja2DivFormRenderer`` transitional form
renderers are deprecated.
Features removed in 5.0
=======================

View File

@ -3,11 +3,14 @@ import unittest
from django.forms.renderers import (
BaseRenderer,
DjangoDivFormRenderer,
DjangoTemplates,
Jinja2,
Jinja2DivFormRenderer,
TemplatesSetting,
)
from django.test import SimpleTestCase
from django.utils.deprecation import RemovedInDjango60Warning
try:
import jinja2
@ -53,3 +56,21 @@ class Jinja2Tests(SharedTests, SimpleTestCase):
class TemplatesSettingTests(SharedTests, SimpleTestCase):
renderer = TemplatesSetting
class DeprecationTests(SimpleTestCase):
def test_django_div_renderer_warning(self):
msg = (
"The DjangoDivFormRenderer transitional form renderer is deprecated. Use "
"DjangoTemplates instead."
)
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
DjangoDivFormRenderer()
def test_jinja2_div_renderer_warning(self):
msg = (
"The Jinja2DivFormRenderer transitional form renderer is deprecated. Use "
"Jinja2 instead."
)
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
Jinja2DivFormRenderer()