Skip to main content

UsernameField

This class provides a specialized form field for handling usernames, extending the standard character field functionality. It automatically performs Unicode NFKC normalization on input values to ensure consistent character representation while protecting against potential performance issues with excessively long strings. Additionally, it configures default widget attributes to disable autocapitalization and enable username-specific autocomplete behavior.

Methods


to_python()

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

Normalizes the input value using NFKC Unicode normalization after standard string conversion. It skips normalization if the value exceeds the maximum length to prevent potential denial-of-service attacks and unnecessary processing of invalid data.

Parameters

NameTypeDescription
valueanyThe raw input value from the form field to be converted and normalized.

Returns

TypeDescription
stringThe Unicode-normalized username string or the original string if it exceeds the maximum length.

widget_attrs()

@classmethod
def widget_attrs(
widget: [Widget](../../../forms/widgets/widget.md?sid=django_forms_widgets_widget)
) - > dict

Updates the widget's HTML attributes to include specific metadata for username fields. It adds 'autocapitalize' set to 'none' and 'autocomplete' set to 'username' to improve the user experience and security in browsers.

Parameters

NameTypeDescription
widget[Widget](../../../forms/widgets/widget.md?sid=django_forms_widgets_widget)The widget instance to which these HTML attributes will be applied.

Returns

TypeDescription
dictA dictionary of HTML attributes to be applied to the form widget.