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
| Attribute | Type | Description |
|---|---|---|
| input_type | string = "checkbox" | The HTML input element type to be rendered, which is set to 'checkbox' for this widget. |
| template_name | string = "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
| Name | Type | Description |
|---|---|---|
| attrs | dict = None | A dictionary containing HTML attributes to be set on the rendered widget. |
| check_test | callable = None | A 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
| Name | Type | Description |
|---|---|---|
| attrs | dict = None | A dictionary of HTML attributes to be added to the rendered input element |
| check_test | callable = None | A 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
| Name | Type | Description |
|---|---|---|
| value | any | The raw data value to be formatted for the HTML value attribute |
Returns
| Type | Description |
|---|---|
str | The 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
| Name | Type | Description |
|---|---|---|
| name | str | The name of the form field |
| value | any | The current value of the field used to determine the checked state |
| attrs | dict | The HTML attributes to be included on the widget element |
Returns
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data (typically request.POST) |
| files | dict | The dictionary of submitted file data (typically request.FILES) |
| name | str | The name of the HTML input element to look up in the data |
Returns
| Type | Description |
|---|---|
bool | The 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data |
| files | dict | The dictionary of submitted file data |
| name | str | The name of the HTML input element |
Returns
| Type | Description |
|---|---|
bool | Always returns False to ensure the checkbox state is processed even when missing from the request |