DateField
This class handles date input validation and conversion, ensuring that provided values are transformed into Python datetime.date objects. It supports multiple input formats and provides default error messaging for invalid entries. The field integrates with a specific date input widget and inherits temporal processing capabilities from a base class.
Attributes
| Attribute | Type | Description |
|---|---|---|
| widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = DateInput | The form widget class used to render this field in HTML, defaulting to DateInput. |
| input_formats | list = formats.get_format_lazy("DATE_INPUT_FORMATS") | A list of formats used to attempt to parse a string into a valid Python datetime.date object. |
| default_error_messages | dict = {"invalid": _("Enter a valid date.")} | A dictionary of error message keys and their corresponding localized text used when validation fails. |
Methods
to_python()
@classmethod
def to_python(
value: any
) - > datetime.date
Validate that the input can be converted to a date. Return a Python datetime.date object.
Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The raw input value to be converted, which can be a string, date, or datetime object. |
Returns
| Type | Description |
|---|---|
datetime.date | A Python date object representing the input value, or None if the value is empty. |
strptime()
@classmethod
def strptime(
value: string,
format: string
) - > datetime.date
Parses a string into a date object using a specific format string.
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The string representation of a date to be parsed. |
| format | string | The strptime-style format string used to interpret the date components. |
Returns
| Type | Description |
|---|---|
datetime.date | A date object extracted from the formatted string. |