Emailaddressattribute
The emailaddressattribute
is a built-in attribute in Django that is used to validate and normalize email addresses. It is a part of Django's built-in form validation system.
Here are some key features of the emailaddressattribute
:
- Validation: The
emailaddressattribute
validates the email address against a set of rules, including:- The email address must contain a local part (the part before the
@
symbol) and a domain (the part after the@
symbol). - The local part must not contain any whitespace characters.
- The domain must not contain any whitespace characters.
- The email address must not contain any invalid characters, such as
@
symbols outside of the local part and domain.
- The email address must contain a local part (the part before the
- Normalization: The
emailaddressattribute
normalizes the email address by:- Converting the email address to lowercase.
- Removing any whitespace characters from the local part and domain.
- Removing any trailing dots (
.
) from the domain.
- Internationalization: The
emailaddressattribute
is designed to work with internationalized email addresses, including those with non-ASCII characters.
To use the emailaddressattribute
in a Django form, you can add it to a forms.EmailField
or forms.EmailInput
widget. For example:
from django import forms
from django.core.validators import EmailAddressValidator
class MyForm(forms.Form):
email = forms.EmailField(validators=[EmailAddressValidator()])
In this example, the EmailAddressValidator
is used to validate the email
field against the rules defined by the emailaddressattribute
. If the email address is invalid, the form will raise a ValidationError
.