Skip to main content

BoundWidget

A container class used for iterating over widgets. This is useful for widgets that have choices. For example, the following can be used in a template:

{% for radio in myform.beatles %} < label for="{{ radio.id_for_label }}" > {{ radio.choice_label }} < span class="radio" >{{ radio.tag }}< /span > < /label > {% endfor %}

Attributes

AttributeTypeDescription
parent_widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget)The parent widget instance that created this bound widget and provides the rendering logic.
datadictA dictionary containing the specific configuration and state for this individual widget choice.
renderer[BaseRenderer](../renderers/baserenderer.md?sid=django_forms_renderers_baserenderer)The engine used to render the widget's HTML output.
template_namestringThe path to the template used for rendering, retrieved from the instance data or the parent widget.
id_for_labelstringThe HTML ID attribute value used to associate a label with this specific widget input.
choice_labelstringThe human-readable text label associated with this specific widget choice.

Constructor

Signature

def BoundWidget(
parent_widget: [Widget](../widgets/widget.md?sid=django_forms_widgets_widget),
data: dict,
renderer: Renderer
)

Parameters

NameTypeDescription
parent_widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget)The parent widget instance associated with this bound widget.
datadictA dictionary containing the widget's configuration and state data.
rendererRendererThe renderer instance used to generate the HTML output.

Methods


tag()

@classmethod
def tag(
wrap_label: boolean = False
) - > string

Renders the widget's HTML using the parent widget's renderer and the current widget's data context.

Parameters

NameTypeDescription
wrap_labelboolean = FalseDetermines whether the widget's label should be included and wrapped within the rendered output.

Returns

TypeDescription
stringThe rendered HTML output for the specific widget instance.

template_name()

@classmethod
def template_name() - > string

Retrieves the template path used to render the widget, prioritizing the instance's specific template over the parent's default.

Returns

TypeDescription
stringThe file path to the Django template used for rendering.

id_for_label()

@classmethod
def id_for_label() - > string

Extracts the unique HTML 'id' attribute from the widget's attributes to be used in associated label tags.

Returns

TypeDescription
stringThe HTML ID string for the widget, or None if no ID is defined.

choice_label()

@classmethod
def choice_label() - > string

Retrieves the human-readable text label associated with this specific widget choice.

Returns

TypeDescription
stringThe display text for the widget option.