SelectDateWidget
A widget that splits date input into three < select > boxes.
Attributes
| Attribute | Type | Description |
|---|---|---|
| none_value | tuple = ("", "---") | A tuple containing the value and label used for the empty choice in the select boxes. |
| month_field | string = "%s_month" | The format string used to generate the HTML name attribute for the month select box. |
| day_field | string = "%s_day" | The format string used to generate the HTML name attribute for the day select box. |
| year_field | string = "%s_year" | The format string used to generate the HTML name attribute for the year select box. |
| template_name | string = "django/forms/widgets/select_date.html" | The path to the template used to render the widget's HTML output. |
| input_type | string = "select" | The type of input used for the widget, which is set to 'select' for dropdown menus. |
| select_widget | class = Select | The widget class used to render the individual year, month, and day select elements. |
| date_re | regex = re.compile(r"(\d{4} | 0)-(\d\d?)-(\d\d?)$") |
| use_fieldset | boolean = true | A 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
| Name | Type | Description |
|---|---|---|
| attrs | dict = None | A dictionary of HTML attributes to be added to the rendered widget. |
| years | `list | tuple` = None |
| months | dict = None | An optional dictionary of months to use in the month select box. |
| empty_label | `str | list |
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
| Name | Type | Description |
|---|---|---|
| name | str | The base name of the form field. |
| value | any | The date value to be displayed in the select boxes. |
| attrs | dict | HTML attributes to be applied to the rendered widget elements. |
Returns
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| value | any | The date object or string to be formatted into component parts. |
Returns
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| id_ | str | The base ID of the widget. |
Returns
| Type | Description |
|---|---|
str | The 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data. |
| files | dict | The dictionary of submitted file data. |
| name | str | The base name of the widget used to identify the sub-fields. |
Returns
| Type | Description |
|---|---|
any | A 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data. |
| files | dict | The dictionary of submitted file data. |
| name | str | The base name of the widget. |
Returns
| Type | Description |
|---|---|
bool | True if none of the date component fields are present in the data, False otherwise. |