I tried what the docs said in here by using whitelist
but it's not working, here's what I tried:
from django.core.validators import EmailValidator
email = models.EmailField(max_length=60, validators=[EmailValidator(whitelist=["gmail", "yahoo", "hotmail"])])
我尝试在gail.com上注册,但是我正常注册了,那么该如何解决
You misunderstand the documentation. The documentation on
whitelist=…
says:This thus means that all domains that contain a dot are considered valid, the
whitelist
only will allow some extra domains (like@localhost
), if you whitelist these.You can make your own validator by subclassing the
EmailValidator
and alter the.validate_domain_part(…)
method [GitHub] however with:So then we can make use of the
WhitelistEmailValidator
to do proper validation.例如: