1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

added new render function for MultiWidget and custom renderings

This commit is contained in:
sschauk 2024-12-08 15:03:33 -05:00
parent c075d4c2c8
commit a64eabd817

View File

@ -913,6 +913,8 @@ class MultiWidget(Widget):
template_name = "django/forms/widgets/multiwidget.html"
use_fieldset = True
use_custom_rendering = True # Default to the new rendering logic
def __init__(self, widgets, attrs=None):
if isinstance(widgets, dict):
@ -923,6 +925,29 @@ class MultiWidget(Widget):
self.widgets = [w() if isinstance(w, type) else w for w in widgets]
super().__init__(attrs)
def render(self, name, value, attrs=None, renderer=None):
"""
Render the multiwidget as a string of HTML by rendering each subwidget.
Removes need for multiwidget.html that is causing multiwidget render issues
Ensures backwards compatability & using template in case there is no custom renderings
"""
if not isinstance(value, (list, tuple)):
value = self.decompress(value)
if self.use_custom_rendering: # New rendering logic
final_attrs = self.build_attrs(attrs or {})
output = []
for i, (widget_name, widget) in enumerate(zip(self.widgets_names, self.widgets)):
widget_value = value[i] if i < len(value) else None
widget_attrs = final_attrs.copy()
widget_attrs["id"] = f"{final_attrs.get('id', name)}_{i}"
output.append(widget.render(f"{name}{widget_name}", widget_value, widget_attrs))
return mark_safe(''.join(output))
else: # Legacy template-based rendering
context = self.get_context(name, value, attrs)
return self._render(self.template_name, context, renderer)
@property
def is_hidden(self):
return all(w.is_hidden for w in self.widgets)