Opened 17 months ago

Closed 17 months ago

Last modified 17 months ago

#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 Natalia Bidart, 17 months ago

Resolution: invalid
Status: newclosed

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 the as_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.

Last edited 17 months ago by Natalia Bidart (previous) (diff)

comment:2 by Petr Dlouhý, 17 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.

in reply to:  2 ; comment:3 by Mariusz Felisiak, 17 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 and django.forms.renderers.Jinja2DivFormRenderer, for the Django and Jinja2 template backends respectively. You can apply one of these via the FORM_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.

in reply to:  3 comment:4 by Petr Dlouhý, 17 months ago

Replying to Mariusz Felisiak:

I'm not sure how it could be clearer.

I see few options:

Directly casting forms to string, e.g. with {{ forms }} will throw a deprecation warning now. It can be resolved either by setting FORM_RENDERER or by explicitly using one of form.as_div(), form.as_ul(), form.as_table(), form.as_p().

Note: See TracTickets for help on using tickets.
Back to Top