Opened 7 weeks ago

Last modified 5 days ago

#35881 assigned Bug

MultiWidget bypasses subwidget rendering customization

Reported by: Adam Johnson Owned by: Alanna Cao
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: David Smith Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
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 (6)

comment:1 by Sarah Boyce, 7 weeks ago

Cc: David Smith added
Triage Stage: UnreviewedAccepted

Thank you for the report Adam

comment:2 by Alanna Cao, 4 weeks ago

Owner: set to Alanna Cao
Status: newassigned

comment:3 by sschauk, 3 weeks ago

Hello! Could you please clarify in which file we can find the individual render methods? Right now, I am looking at django/django/forms/widgets.py where all the different render, render_js, and render_css functions are but I don't think that's what you are referring to. I am also not quite sure how the HTML code is linked to the multi widget function or current sub widget functions. Any clarification would be very helpful!

(to clarify, I am collaborating with Alanna on this ticket)

comment:4 by Adam Johnson, 3 weeks ago

MultiWidget is in django/forms/widgets.py. Its render() method is Widget.render() in the same file. That renders the template declared within MultiWidget.template_name which is django/forms/widgets/multiwidget.html. There are Django Template Language and Jinja implementations of that template, but they both end up using {% include %}, which is where the issue comes from.

Implementing a new render() within MultiWidget is one of the solutions I proposed.

comment:5 by Alanna Cao, 2 weeks ago

Has patch: set

comment:6 by Sarah Boyce, 5 days ago

Needs tests: set
Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top