Skip to main content

CheckboxInput

This class represents an HTML checkbox input widget for use in web forms. It handles the logic for determining if a checkbox should be checked based on a provided value or a custom callable test. Additionally, it manages the translation of form submission data into boolean values, accounting for the fact that unselected checkboxes are omitted from standard HTML form data.

Attributes

AttributeTypeDescription
input_typestring = "checkbox"The HTML input element type to be rendered, which is set to 'checkbox' for this widget.
template_namestring = "django/forms/widgets/checkbox.html"The path to the Django HTML template used to render the checkbox widget.

Constructor

Signature

def CheckboxInput(
attrs: dict = None,
check_test: callable = None
)

Parameters

NameTypeDescription
attrsdict = NoneA dictionary containing HTML attributes to be set on the rendered widget.
check_testcallable = NoneA callable that takes a value and returns True if the checkbox should be checked; defaults to boolean_check if not provided.

Signature

def CheckboxInput(
attrs: dict = None,
check_test: callable = None
)

Parameters

NameTypeDescription
attrsdict = NoneA dictionary of HTML attributes to be added to the rendered input element
check_testcallable = NoneA callable that accepts a value and returns a boolean indicating if the checkbox should be rendered as checked

Methods


format_value()

@classmethod
def format_value(
value: any
) - > str

Only return the 'value' attribute if value isn't empty.

Parameters

NameTypeDescription
valueanyThe raw data value to be formatted for the HTML value attribute

Returns

TypeDescription
strThe string representation of the value, or None if the value is a boolean, empty string, or null

get_context()

@classmethod
def get_context(
name: str,
value: any,
attrs: dict
) - > dict

Prepares the dictionary of data required to render the widget template, including the logic to set the 'checked' attribute.

Parameters

NameTypeDescription
namestrThe name of the form field
valueanyThe current value of the field used to determine the checked state
attrsdictThe HTML attributes to be included on the widget element

Returns

TypeDescription
dictA dictionary containing the template context, including updated HTML attributes

value_from_datadict()

@classmethod
def value_from_datadict(
data: dict,
files: dict,
name: str
) - > bool

Extracts the checkbox value from submitted form data, converting missing keys to False and handling string-to-boolean translations.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data (typically request.POST)
filesdictThe dictionary of submitted file data (typically request.FILES)
namestrThe name of the HTML input element to look up in the data

Returns

TypeDescription
boolThe boolean representation of the checkbox state based on the submitted data

value_omitted_from_data()

@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: str
) - > bool

Determines if the value is missing from the submission; always returns False because unchecked checkboxes are omitted by design in HTML.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data
filesdictThe dictionary of submitted file data
namestrThe name of the HTML input element

Returns

TypeDescription
boolAlways returns False to ensure the checkbox state is processed even when missing from the request