Skip to main content

Widget

This class provides a base implementation for HTML form widgets, handling the rendering of data into HTML and the extraction of values from submitted data. It manages widget attributes, localization, and template context generation to ensure consistent behavior across different input types. Key features include support for multipart forms, subwidget iteration, and custom attribute building for HTML elements.

Attributes

AttributeTypeDescription
needs_multipart_formboolean = FalseDetermines does this widget need multipart form
is_localizedboolean = FalseBoolean flag indicating if the widget's value should be localized using current locale settings during rendering.
is_requiredboolean = FalseBoolean flag that determines if the widget is rendered with a required HTML attribute.
supports_microsecondsboolean = TrueBoolean flag indicating whether the widget is capable of handling and displaying microsecond precision for temporal values.
use_fieldsetboolean = FalseBoolean flag determining whether the widget should be grouped within a fieldset element when rendered.

Constructor

Signature

def Widget(
attrs: dict = None
)

Parameters

NameTypeDescription
attrsdict = NoneA dictionary containing HTML attributes to be set on the rendered widget.

Methods


is_hidden()

@classmethod
def is_hidden() - > boolean

Determines if the widget is of a hidden input type, which affects how it is rendered and whether it uses required attributes.

Returns

TypeDescription
booleanTrue if the widget's input type is 'hidden', otherwise False.

subwidgets()

@classmethod
def subwidgets(
name: string,
value: any,
attrs: dict
) - > generator

Yields the subwidgets that compose this widget, typically used for complex widgets like radio buttons or checkboxes.

Parameters

NameTypeDescription
namestringThe name of the field to be used in the subwidget context.
valueanyThe value to be formatted and assigned to the subwidgets.
attrsdictAdditional HTML attributes to be applied to the subwidgets.

Returns

TypeDescription
generatorA generator yielding context dictionaries for each subwidget.

format_value()

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

Return a value as it should appear when rendered in a template.

Parameters

NameTypeDescription
valueanyThe raw data value to be formatted for display in the HTML template.

Returns

TypeDescription
stringThe localized or string-converted value, or None if the input is empty.

get_context()

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

Constructs the dictionary of data required to render the widget template, including attributes, name, and formatted value.

Parameters

NameTypeDescription
namestringThe name of the form field.
valueanyThe value of the field to be rendered.
attrsdictHTML attributes to merge with the widget's default attributes.

Returns

TypeDescription
dictA dictionary containing the widget's state and configuration for template rendering.

render()

@classmethod
def render(
name: string,
value: any,
attrs: dict,
renderer: object
) - > string

Render the widget as an HTML string.

Parameters

NameTypeDescription
namestringThe name of the form field.
valueanyThe value to be displayed in the widget.
attrsdictOptional HTML attributes to include on the rendered element.
rendererobjectThe template renderer engine used to generate the HTML output.

Returns

TypeDescription
stringA safe HTML string representing the widget.

build_attrs()

@classmethod
def build_attrs(
base_attrs: dict,
extra_attrs: dict
) - > dict

Build an attribute dictionary.

Parameters

NameTypeDescription
base_attrsdictThe primary dictionary of HTML attributes.
extra_attrsdictAdditional attributes to merge into or override the base attributes.

Returns

TypeDescription
dictA merged dictionary containing both base and extra HTML attributes.

value_from_datadict()

@classmethod
def value_from_datadict(
data: dict,
files: dict,
name: string
) - > any

Given a dictionary of data and this widget's name, return the value of this widget or None if it's not provided.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data (e.g., request.POST).
filesdictThe dictionary of submitted file data (e.g., request.FILES).
namestringThe name of the widget used as the key in the data dictionary.

Returns

TypeDescription
anyThe value associated with the widget name from the submitted data.

value_omitted_from_data()

@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: string
) - > boolean

Checks if the widget's name is missing from the provided data dictionary, indicating the field was not submitted.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data.
filesdictThe dictionary of submitted file data.
namestringThe name of the widget to check for in the data.

Returns

TypeDescription
booleanTrue if the name is not present in the data, False otherwise.

id_for_label()

@classmethod
def id_for_label(
id_: string
) - > string

Return the HTML ID attribute of this Widget for use by a < label >, given the ID of the field. Return an empty string if no ID is available.

Parameters

NameTypeDescription
id_stringThe base ID of the form field.

Returns

TypeDescription
stringThe ID string to be used in a label's 'for' attribute.

use_required_attribute()

@classmethod
def use_required_attribute(
initial: any
) - > boolean

Determines whether the widget should include the HTML 'required' attribute based on its visibility.

Parameters

NameTypeDescription
initialanyThe initial value of the field, used to determine if the required attribute is necessary.

Returns

TypeDescription
booleanTrue if the widget is not hidden and should use the required attribute, otherwise False.