#34531 closed Uncategorized (invalid)
Rendering form throws deprecation warning
Reported by: | Petr Dlouhý | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When I execute following code:
# deprecation.py from django.forms import Form from django.template import Template, Context t = Template("{{ form }}") c = Context({"form": Form}) t.render(c)
with
python -Wall -W warning::DeprecationWarning ./manage.py shell < deprecation.py
I git following error:
.../lib/python3.11/site-packages/django/template/base.py:1047: RemovedInDjango50Warning: 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/4.2/releases/4.1/ for more details value = str(value)
I think this is incorrect behavior, since the deprecated code is contained only in Django (django/forms/renders.py line 21) and not in the users code.
This breaks automated tests which should ensure future Django compatibility. It is possible to fix them only by some hacks.
Django version: 4.2
Python version: 3.11.3
Change History (4)
comment:1 by , 19 months ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
follow-up: 3 comment:2 by , 19 months ago
Hi Natalia,
thank you very much for your comment.
This clears things up. At the same time I am still a bit confused about what is the deprecation message saying regarding this case.
I confess that I didn't read the whole release notes but only searched for default.html
keyword which found me one paragraph in the Miscellaneous section which didn't help me much.
Even now when I read the Forms section I would be confused what should I do if I haven't read your comment.
Also it is very hard to find the exact place in the template where the {{ form }}
tag does occur since the deprecation message doesn't mention anything about the template. I had to use pudb
very extensively to learn what is happening.
I suggest at least adding some more explicit info in the release notes regarding this case.
If you are interested, I could try to formulate something.
follow-up: 4 comment:3 by , 19 months ago
Replying to Petr Dlouhý:
Even now when I read the Forms section I would be confused what should I do if I haven't read your comment.
This note is crucial:
"In order to smooth adoption of the new
<div>
output style, two transitional form renderer classes are available:django.forms.renderers.DjangoDivFormRenderer
anddjango.forms.renderers.Jinja2DivFormRenderer
, for the Django and Jinja2 template backends respectively. You can apply one of these via theFORM_RENDERER
setting. For example:"
FORM_RENDERER = "django.forms.renderers.DjangoDivFormRenderer""Once the <div> output style is the default, from Django 5.0, these transitional renderers will be deprecated, for removal in Django 6.0. The
FORM_RENDERER
declaration can be removed at that time."
I'm not sure how it could be clearer.
comment:4 by , 19 months ago
Replying to Mariusz Felisiak:
I'm not sure how it could be clearer.
I see few options:
- Change link in the deprecation message from https://docs.djangoproject.com/en/4.2/releases/4.1/ to https://docs.djangoproject.com/en/4.2/releases/4.1/#forms to point users to the right section.
- There is no explicit note that casting form to string will raise deperecation warning. I would add an explicit bullet regarding
{{ form }}
tags especially for developers of third party applications (where settingFORM_RENDERER
might not be viable option) saying something like:
Directly casting forms to string, e.g. with
{{ forms }}
will throw a deprecation warning now. It can be resolved either by settingFORM_RENDERER
or by explicitly using one ofform.as_div()
,form.as_ul()
,form.as_table()
,form.as_p()
.
Hello Petr! I believe the warning is necessary because Django users updating from Django 4.2 to Django 5 should take an explicit action regarding forms. If no action is taken, existing Django 4.2 projects risk to have rendering issues for existing forms.
The action is simple: the Django user has to either define
template_name
in their form, or render the form in a template explicitly showing a format. So, if you change your example to any of theas_p
,as_table
,as_div
, etc:t = Template("{{ form.as_p }}")
`
the warning is resolved and no issues are reported. I'll close as invalid considering the above.