Opened 5 months ago

Closed 5 months ago

#33445 closed New feature (needsinfo)

MultiWidget doesn't call subwidgets' render() methods.

Reported by: Maxim Danilov Owned by: nobody
Component: Forms Version: 4.0
Severity: Normal Keywords: Forms, multiwidget, subwidget render.
Cc: Matthias Kestenholz, David Smith Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Subwidget don't rendered correctly in Miltiwidget, if Subwidget have own render method.

How i found it: In Multiwidget I use CKEditorWidget(forms.Textarea), whose have own render.

How to reproduce error:

Put MyWidget like a subwidget in multiwidget.

Class MyWidget(TextInput):
    def render(self, *args, **kwargs):
        print('hello world')
        return super().render(*args, **kwargs)

Nobody can see "Hello world" after Multiwidget render.

How i patch it - add subwidget.render call in context.

# in Multiwidget:
def get_context(self, name, value, attrs):
context['widget']['subwidgets'] = (self.rednder_subwidget(subwidget, **subwidget_context) for subwidget, subwidget_context in zip(self.widgets, context['widget']['subwidgets']))
# add
    def rednder_subwidget(subwidget, **kwargs):
        kwargs['render'] = lambda: mark_safe(subwidget.render(kwargs['name'], kwargs['value'], attrs=kwargs['attrs'], renderer=kwargs.get('renderer')))
        return kwargs

in multiwitget template:
{% spaceless %}{% for widget in widget.subwidgets %}{{widget.render}}{% endfor %}{% endspaceless %}

Other possibility - write in documentation: MultiWidget works only with native Built-in Django widgets.

Change History (1)

comment:1 Changed 5 months ago by Mariusz Felisiak

Cc: Matthias Kestenholz David Smith added
Easy pickings: unset
Has patch: unset
Resolution: needsinfo
Status: newclosed
Summary: subwidget don't rendered correctly in Miltiwidget, if subwidget have own render method.MultiWidget doesn't call subwidgets' render() methods.
Type: BugNew feature

Thanks for this report, however it's not a bug, IMO. MultiWidget is also a normal Widget and getting context for subwidgets (as documented) should be sufficient for rendering. render() is a highly specialized method and I don't see why calling a custom render() for subwidgets would be necessary. Also, as far as I'm aware CKEditorWidget doesn't have a render() method.

What's your use case?

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