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
| Name | Type | Description |
|---|---|---|
| value | any | The raw input value from the form field to be converted and normalized. |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| widget | [Widget](../../../forms/widgets/widget.md?sid=django_forms_widgets_widget) | The widget instance to which these HTML attributes will be applied. |
Returns
| Type | Description |
|---|---|
dict | A dictionary of HTML attributes to be applied to the form widget. |