[Django] #34531: Rendering form throws deprecation warning

150 views
Skip to first unread message

Django

unread,
May 2, 2023, 10:10:43 AM5/2/23
to django-...@googlegroups.com
#34531: Rendering form throws deprecation warning
-----------------------------------------+------------------------
Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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

--
Ticket URL: <https://code.djangoproject.com/ticket/34531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 2, 2023, 1:51:26 PM5/2/23
to django-...@googlegroups.com
#34531: Rendering form throws deprecation warning
-------------------------------+--------------------------------------

Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 4.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* status: new => closed
* resolution: => invalid


Comment:

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:

```python
t = Template("{{ form.as_p }}")
```

the warning is resolved and no issues are reported. I'll close as invalid
considering the above.

--
Ticket URL: <https://code.djangoproject.com/ticket/34531#comment:1>

Django

unread,
May 3, 2023, 4:25:05 AM5/3/23
to django-...@googlegroups.com
#34531: Rendering form throws deprecation warning
-------------------------------+--------------------------------------
Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 4.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Petr Dlouhý):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34531#comment:2>

Django

unread,
May 3, 2023, 4:44:07 AM5/3/23
to django-...@googlegroups.com
#34531: Rendering form throws deprecation warning
-------------------------------+--------------------------------------
Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 4.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:2 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34531#comment:3>

Django

unread,
May 3, 2023, 9:57:07 AM5/3/23
to django-...@googlegroups.com
#34531: Rendering form throws deprecation warning
-------------------------------+--------------------------------------
Reporter: Petr Dlouhý | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 4.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Petr Dlouhý):

Replying to [comment:3 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
setting `FORM_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 setting
`FORM_RENDERER` or by explicitly using one of `form.as_div()`,
`form.as_ul()`, `form.as_table()`, `form.as_p()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/34531#comment:4>

Reply all
Reply to author
Forward
0 new messages