[Django] #33445: subwidget don't rendered correctly in Miltiwidget, if subwidget have own render method.

1 view
Skip to first unread message

Django

unread,
Jan 16, 2022, 5:48:38 PMJan 16
to django-...@googlegroups.com
#33445: subwidget don't rendered correctly in Miltiwidget, if subwidget have own
render method.
-------------------------------------+-------------------------------------
Reporter: Maxim | Owner: nobody
Danilov |
Type: Bug | Status: new
Component: Forms | Version: 4.0
Severity: Normal | Keywords: Forms, multiwidget,
Triage Stage: | subwidget render.
Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
@staticmethod
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.

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

Django

unread,
Jan 17, 2022, 12:51:11 AMJan 17
to django-...@googlegroups.com
#33445: MultiWidget doesn't call subwidgets' render() methods.
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 4.0
Severity: Normal | Resolution: needsinfo

Keywords: Forms, multiwidget, | Triage Stage:
subwidget render. | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* cc: Matthias Kestenholz, David Smith (added)
* type: Bug => New feature
* easy: 1 => 0
* has_patch: 1 => 0
* resolution: => needsinfo


Comment:

Thanks for this report, however it's not a bug, IMO. `MultiWidget` is also
a normal `Widget` and getting context for subwidgets (as
[https://docs.djangoproject.com/en/4.0/ref/forms/widgets/#django.forms.MultiWidget.get_context
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
[https://github.com/django-ckeditor/django-
ckeditor/blob/50cd4aba9533c541e7d1ef5a4e67aed8c827f10c/ckeditor/widgets.py#L28-L137
CKEditorWidget] doesn't have a `render()` method.

What's your use case?

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

Reply all
Reply to author
Forward
0 new messages