Opened 3 weeks ago

Last modified 2 weeks ago

#35881 new Bug

MultiWidget bypasses subwidget rendering customization

Reported by: Adam Johnson Owned by:
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: David Smith Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Widget.render is documented as the place to override rendering behaviour, with the top of Widget docs saying:

You may also implement or override the render() method on custom widgets.

On top of this, the renderer API is touted as another way to customize how widgets are rendered.

MultiWidget bypasses both of these for its subwidgets. Rather than go through their render() methods, it uses a template that just includes the subwidget templates:

{% spaceless %}{% for widget in widget.subwidgets %}{% include widget.template_name %}{% endfor %}{% endspaceless %}

I encountered this issue on a project with custom templates, where a MultiValueField from a third-party package dropped the custom styles.

One solution could be to make a MultiWidget.render() method that calls each subwidget's render() method and glues the results together.

Another would be to make the existing MultiWidget.get_context pass each subwidget's render method into the context, and then the template could call it.

One backwards compatibility concern is continuing to work if the user has customized multiwidget.html, where they may be relying on the old context data and using {% include subwidget.template_name %}.

Change History (1)

comment:1 by Sarah Boyce, 2 weeks ago

Cc: David Smith added
Triage Stage: UnreviewedAccepted

Thank you for the report Adam

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