Skip to main content

SelectDateWidget

A widget that splits date input into three < select > boxes.

Attributes

AttributeTypeDescription
none_valuetuple = ("", "---")A tuple containing the value and label used for the empty choice in the select boxes.
month_fieldstring = "%s_month"The format string used to generate the HTML name attribute for the month select box.
day_fieldstring = "%s_day"The format string used to generate the HTML name attribute for the day select box.
year_fieldstring = "%s_year"The format string used to generate the HTML name attribute for the year select box.
template_namestring = "django/forms/widgets/select_date.html"The path to the template used to render the widget's HTML output.
input_typestring = "select"The type of input used for the widget, which is set to 'select' for dropdown menus.
select_widgetclass = SelectThe widget class used to render the individual year, month, and day select elements.
date_reregex = re.compile(r"(\d{4}0)-(\d\d?)-(\d\d?)$")
use_fieldsetboolean = trueA boolean indicating whether the widget should be wrapped in a fieldset element for accessibility.

Constructor

Signature

def SelectDateWidget(
attrs: dict = None,
years: list|tuple = None,
months: dict = None,
empty_label: str|list|tuple = None
) - > null

Parameters

NameTypeDescription
attrsdict = NoneA dictionary of HTML attributes to be added to the rendered widget.
years`listtuple` = None
monthsdict = NoneAn optional dictionary of months to use in the month select box.
empty_label`strlist

Methods


get_context()

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

Generates the template context for the widget, including the individual sub-widgets for year, month, and day selection.

Parameters

NameTypeDescription
namestrThe base name of the form field.
valueanyThe date value to be displayed in the select boxes.
attrsdictHTML attributes to be applied to the rendered widget elements.

Returns

TypeDescription
dictA dictionary containing the widget context and a list of sub-widgets ordered by the current locale's date format.

format_value()

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

Return a dict containing the year, month, and day of the current value. Use dict instead of a datetime to allow invalid dates such as February 31 to display correctly.

Parameters

NameTypeDescription
valueanyThe date object or string to be formatted into component parts.

Returns

TypeDescription
dictA dictionary with 'year', 'month', and 'day' keys representing the components of the date.

id_for_label()

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

Returns the HTML ID of the first select element in the widget, ensuring labels point to the correct sub-component.

Parameters

NameTypeDescription
id_strThe base ID of the widget.

Returns

TypeDescription
strThe HTML ID attribute for the first date component select box.

value_from_datadict()

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

Extracts and combines the year, month, and day values from the submitted data into a single date string or object.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data.
filesdictThe dictionary of submitted file data.
namestrThe base name of the widget used to identify the sub-fields.

Returns

TypeDescription
anyA formatted date string, a datetime.date object, or a pseudo-ISO date string if the input is invalid.

value_omitted_from_data()

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

Checks if all three date components (year, month, and day) are missing from the submitted data.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data.
filesdictThe dictionary of submitted file data.
namestrThe base name of the widget.

Returns

TypeDescription
boolTrue if none of the date component fields are present in the data, False otherwise.