summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorDavid Smith <smithdc@gmail.com>2022-05-05 14:26:09 +0200
committerCarlton Gibson <carlton.gibson@noumenal.es>2022-05-17 11:16:54 +0200
commitd126eba3637d84caa80fa4258e2e59ef07a8260c (patch)
treee135d40a0934710bae8dcb7e912b49695e600adb /django/forms
parent6af8673255ad2aca01097db9a8c64dc6b31678c0 (diff)
Refs #32339 -- Deprecated default.html form template.
Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/renderers.py28
-rw-r--r--django/forms/utils.py28
2 files changed, 51 insertions, 5 deletions
diff --git a/django/forms/renderers.py b/django/forms/renderers.py
index 0e406c9c7e..43340c6c88 100644
--- a/django/forms/renderers.py
+++ b/django/forms/renderers.py
@@ -15,6 +15,9 @@ 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"
@@ -64,6 +67,31 @@ class Jinja2(EngineMixin, BaseRenderer):
return Jinja2
+class DjangoDivFormRenderer(DjangoTemplates):
+ """
+ Load Django templates from django/forms/templates and from apps'
+ 'templates' directory and use the 'div.html' template to render forms and
+ 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"
+
+
+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"
+
+
class TemplatesSetting(BaseRenderer):
"""
Load templates using template.loader.get_template() which is configured
diff --git a/django/forms/utils.py b/django/forms/utils.py
index 77678054db..905babce4d 100644
--- a/django/forms/utils.py
+++ b/django/forms/utils.py
@@ -1,13 +1,16 @@
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):
@@ -42,6 +45,16 @@ 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(
@@ -49,12 +62,17 @@ class RenderableMixin:
)
def render(self, template_name=None, context=None, renderer=None):
- return mark_safe(
- (renderer or self.renderer).render(
- template_name or self.template_name,
- context or self.get_context(),
+ 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
__html__ = render