From b209518089131c6b4afd18b1d9c320ba3521c5ab Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 18 Jan 2023 11:08:39 +0100 Subject: [PATCH] Refs #32339 -- Deprecated transitional form renderers. --- django/forms/renderers.py | 26 ++++++++++++++++------- docs/internals/deprecation.txt | 3 +++ docs/ref/forms/renderers.txt | 23 ++++---------------- docs/releases/4.1.txt | 6 +++--- docs/releases/5.0.txt | 3 ++- tests/forms_tests/tests/test_renderers.py | 21 ++++++++++++++++++ 6 files changed, 51 insertions(+), 31 deletions(-) diff --git a/django/forms/renderers.py b/django/forms/renderers.py index d53a7c633de..30f8141dee4 100644 --- a/django/forms/renderers.py +++ b/django/forms/renderers.py @@ -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): diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 1f07f3f39b8..9ada56cf6c6 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -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 ` for more details on these changes. +* The ``DjangoDivFormRenderer`` and ``Jinja2DivFormRenderer`` transitional form + renderers will be removed. + .. _deprecation-removed-in-5.1: 5.1 diff --git a/docs/ref/forms/renderers.txt b/docs/ref/forms/renderers.txt index 92ec2d68df9..425594b4702 100644 --- a/docs/ref/forms/renderers.txt +++ b/docs/ref/forms/renderers.txt @@ -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 ``
`` 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 ``
`` 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`` -------------------- diff --git a/docs/releases/4.1.txt b/docs/releases/4.1.txt index 150195727e5..124663b5e3f 100644 --- a/docs/releases/4.1.txt +++ b/docs/releases/4.1.txt @@ -233,9 +233,9 @@ Forms * In order to smooth adoption of the new ``
`` 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:: diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt index 78148c79e86..a1dc6a1c1c8 100644 --- a/docs/releases/5.0.txt +++ b/docs/releases/5.0.txt @@ -247,7 +247,8 @@ Features deprecated in 5.0 Miscellaneous ------------- -* ... +* The ``DjangoDivFormRenderer`` and ``Jinja2DivFormRenderer`` transitional form + renderers are deprecated. Features removed in 5.0 ======================= diff --git a/tests/forms_tests/tests/test_renderers.py b/tests/forms_tests/tests/test_renderers.py index 3e973ad8fc0..54e20626afe 100644 --- a/tests/forms_tests/tests/test_renderers.py +++ b/tests/forms_tests/tests/test_renderers.py @@ -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()