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
| Attribute | Type | Description |
|---|---|---|
| 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. |
| data | dict | A 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_name | string | The path to the template used for rendering, retrieved from the instance data or the parent widget. |
| id_for_label | string | The HTML ID attribute value used to associate a label with this specific widget input. |
| choice_label | string | The 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
| Name | Type | Description |
|---|---|---|
| parent_widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) | The parent widget instance associated with this bound widget. |
| data | dict | A dictionary containing the widget's configuration and state data. |
| renderer | Renderer | The 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
| Name | Type | Description |
|---|---|---|
| wrap_label | boolean = False | Determines whether the widget's label should be included and wrapped within the rendered output. |
Returns
| Type | Description |
|---|---|
string | The 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
| Type | Description |
|---|---|
string | The 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
| Type | Description |
|---|---|
string | The 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
| Type | Description |
|---|---|
string | The display text for the widget option. |