简体   繁体   中英

"email": [ "Enter a valid email address."] Django is saying email is not an email

problem:

I am trying to serialize an email field using Django Rest Framework, however the server is saying not accepting any email. All emails are greeted with error "Enter a valid email address". Below are my configurations.

serializers.py

class UserSerializer(serializers.ModelSerializer):

class Meta:
    model = User
    fields = ('email', 'password')

models.py

class User(AbstractBaseUser, PermissionsMixin):

   email = models.EmailField(unique=True, verbose_name='email address')
   first_name = models.CharField(max_length=256, verbose_name="First Name", blank=True)
   last_name = models.CharField(max_length=256, verbose_name="Last Name", blank=True)
   id = models.UUIDField(primary_key=True, unique=True)
   data = JSONField(default=default_data, name="device_data")
   is_staff = models.BooleanField(default=False)
   is_active = models.BooleanField(default=True)
   date_joined = models.DateTimeField(default=timezone.now)

   objects = UserManager()

   USERNAME_FIELD = 'email'
   REQUIRED_FIELDS = []

django.core.validators.py

@deconstructible
class EmailValidator:
     message = _('Enter a valid email address. Not valid')
     code = 'invalid'
     ...
     domain_whitelist = ['localhost', 'gmail', 'gmail.com', '@gmail.com']

httpie to server

  http -f POST http://127.0.0.1:8000/reg email="l55@gmail.com", password='pw'

response

HTTP/1.1 400 Bad Request
Allow: POST, OPTIONS
Content-Length: 42
Content-Type: application/json
Date: Mon, 30 Mar 2020 00:11:14 GMT
Server: WSGIServer/0.2 CPython/3.7.3
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
     "email": [
        "Enter a valid email address."
     ]
 }

Previous effort:

  1. I was told to add domains to the domain_whitlist property (which i've done)
  2. I have also tried using Django's default user class, but I encountered the same error.

Can you help me?

Thank you!

In your httpie call you have a comma after the email address, this is being appended to the email. You need to remove it, l55@gmail.com, is not a valid email address

http -f POST http://127.0.0.1:8000/reg email="l55@gmail.com" password='pw'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM