Django EmailField accepts invalid values

I am currently using the default EmailField attribute for my form. The problem I am facing is that the form considers the email, such as name@mail.56 , valid. Do I need to implement my own validators in this field in order for it to work correctly?

I got the impression that having:

 #models.py email = models.EmailField(max_length=254, blank=False, unique=True, error_messages={'required': 'Please provide your email address.', 'unique': 'An account with this email exist.'},) 

Or having:

 #forms.py email = forms.EmailField() 

will take care of this type of check for me, but it doesn't look like that.

+8
django django-forms
source share
3 answers

Indeed, name@mail.56 email is a valid email address for django EmailValidator , does not see errors:

 >>> from django.core.validators import validate_email >>> validate_email("name@mail.56") >>> 

Django (1.5.1) uses the following regular expression to validate an email address:

 r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom # quoted-string, see also http://tools.ietf.org/html/rfc2822#section-3.2.5 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[AZ]{2,6}\.?|[A-Z0-9-]{2,}\.?)$)' # domain r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$' 

And it actually complies with the RFC2822 standard.

If you want to make name@mail.56 crash during validation, you can create your own validator and add it to EmailField validators with the built-in validate_email validator, for example:

 from django.core.validators import validate_email from django.core.exceptions import ValidationError def custom_validate_email(value): if <custom_check>: raise ValidationError('Email format is incorrect') ... email = models.EmailField(max_length=254, blank=False, unique=True, validators=[validate_email, custom_validate_email) 

And, FYI, you can always submit a ticket to the django ticket system or ask about a problem on the IRC django channel (irc: //irc.freenode.net/django).

See also: Writing Validators .

Hope this helps.

+12
source share

Here you can see the regex used.

I think that it does not reject 100% of the wrong letters. Therefore, the docs say:

Confirms that this value is a valid email address using a moderately complex regular expression.

What I understand from this is that it does not do a perfect check due to the design decision (it will be a performance compromise).

Finally, I'm sure your example name@mail.56 is a valid email address . The domain part of an email address can be IP (both IPv4 and IPv6) or a host name. See here for more information on the topic.

+4
source share

Only to check the DB level you will need to call full_clean manually.

Three important quotes from the documentation:

How validators work

See form validation for more information on how validators work in forms and Validating objects for how they run in models. Note that the validators will not start automatically when you save the model, but if you use ModelForm, it will run your validators on any fields that are included in your form. See the ModelForm documentation for information on how model validation interacts with forms.

Model.clean_fields

The second step, full_clean (), makes a call to Model.clean (). This method must be overridden to perform custom validation on your model.

Model.full_clean

Note that full_clean () will not be called automatically when your save () method is called. You will need to call it manually when you want to perform a one-step model check for your own manually created model.

 from django.db import models class MyTable(models.Model): email = models.EmailField(unique=True) def save(self, *args, **kwargs): super().full_clean() super().save(*args, **kwargs) 
0
source share

All Articles