Skip to main content

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

AttributeTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = DateInputThe form widget class used to render this field in HTML, defaulting to DateInput.
input_formatslist = 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_messagesdict = {"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

NameTypeDescription
valueanyThe raw input value to be converted, which can be a string, date, or datetime object.

Returns

TypeDescription
datetime.dateA 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

NameTypeDescription
valuestringThe string representation of a date to be parsed.
formatstringThe strptime-style format string used to interpret the date components.

Returns

TypeDescription
datetime.dateA date object extracted from the formatted string.