Skip to main content

RadioSelect

This class provides a widget for rendering a group of radio buttons as a single selection form field. It utilizes specific HTML templates for the radio group and individual options, ensuring structured output through the use of a fieldset. The class also includes specialized logic for label identification to enhance accessibility for screen reader users.

Attributes

AttributeTypeDescription
input_typestring = radioThe HTML input type attribute used for the widget, which is set to "radio".
template_namestring = django/forms/widgets/radio.htmlThe path to the Django HTML template used to render the radio group container.
option_template_namestring = django/forms/widgets/radio_option.htmlThe path to the Django HTML template used to render each individual radio option.
use_fieldsetboolean = trueA boolean flag indicating that the widget should be wrapped in a fieldset element for improved accessibility and grouping.

Constructor

Signature

def RadioSelect()

Methods


id_for_label()

@classmethod
def id_for_label(
id_: string,
index: string = None
) - > string

Don't include for="field_0" in < label > to improve accessibility when using a screen reader, in addition clicking such a label would toggle the first input.

Parameters

NameTypeDescription
id_stringThe base ID of the widget field used to generate unique identifiers for sub-widgets.
indexstring = NoneThe specific index of the radio option within the choice set used to suffix the base ID.

Returns

TypeDescription
stringThe HTML ID attribute value for a specific radio option, or an empty string if no index is provided to prevent incorrect label association.