Opened 4 years ago

Closed 4 years ago

#31722 closed Uncategorized (invalid)

RangeWidget produces 1 subwidget, itself

Reported by: Ryan Heard Owned by: nobody
Component: Uncategorized Version: 3.0
Severity: Normal Keywords: MultiWidget RangeWidget Widget
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The RangeWidget cannot be iterated over correctly, as it returns a list of itself for it's subwidgets.

{% for subwidget in form.date_range.subwidgets %}
  {{ forloop.counter }}
  {{ subwidget }}
{% endfor %}

I would expect that to output "1" then the start widget, then "2" then the end widget. However it only prints "1" and then both widgets, since the subwidget is really the current widget.

Change History (1)

comment:1 by Carlton Gibson, 4 years ago

Resolution: invalid
Status: newclosed

You need to drill down further to access the child widgets.

form.date_range is a BoundField. subwidgets returns the single item list of the BoundWidget wrapping your RangeWidget, which is a MultiWidget subclass. The RangeWidget has a widgets property containing the child widgets you're after.

As per the multi widget docs RangeWidget will add subwidgets to the context to be used by the widget template:

>>>f["a_range"].subwidgets[0].parent_widget.get_context('a_range', '', {})["widget"]["subwidgets"]
[{'name': 'a_range_0', 'is_hidden': False, 'required': False, 'value': None, 'attrs': {}, 'template_name': 'django/forms/widgets/number.html', 'type': 'number'}, {'name': 'a_range_1', 'is_hidden': False, 'required': False, 'value': None, 'attrs': {}, 'template_name': 'django/forms/widgets/number.html', 'type': 'number'}]

It's that that can be iterated as you're intending. Please see TicketClosingReasons/UseSupportChannels.

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