Opened 12 months ago
Last modified 6 months ago
#35881 assigned Bug
MultiWidget bypasses subwidget rendering customization
| Reported by: | Adam Johnson | Owned by: | Ahmed Nassar |
|---|---|---|---|
| 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 (8)
comment:1 by , 12 months ago
| Cc: | added |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 11 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:3 by , 11 months 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 , 11 months 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 , 11 months ago
| Has patch: | set |
|---|
comment:6 by , 11 months ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
comment:7 by , 6 months ago
I'll work on this ticket and submit my PR. Due to the owner's inactivity.
comment:8 by , 6 months ago
| Owner: | changed from to |
|---|
Thank you for the report Adam