pythondjangobackendcrmemail-validation

Django Email Domain Validation with cleaned_email function works very slow after submitting


Hi i am trying to write email domain validation for sign up form in django syntax with following function code. I am using the function inside class meta:

VALID_DOMAINS is a list = ['gmail.com', 'hotmail.com']
I am using Edge browser for test

    def clean_email(self, *args, **kwargs):
        super(SignUpForm, self).__init__(*args, **kwargs)
        original_email = self.cleaned_data['email']
        domain = self.cleaned_data["email"].split("@")[-1]
        if domain not in VALID_DOMAINS:
            raise ValidationError("E-mail addresses from %(domain)s are not allowed.", code="invalid", params={"domain": domain})
        
    return original_email

Problems are:

  1. After typing this function, sign up page works very slow when trying to check with any wrong domain (any outside of VALID_DOMAINS). (after submitting the form)

  2. After submitting sign up form with the wrong email domain, sign up from is not returning its original shape which is before submitting. There are also happening small corruptions, and password fields act as if passwords are not typed.

I was not expecting to work very slow after submitting form.


Solution

  • You call the __init__ function of the form, which does not make much sense:

    def clean_email(self, *args, **kwargs):
        # super(SignUpForm, self).__init__(*args, **kwargs)
        original_email = self.cleaned_data['email']
        domain = self.cleaned_data["email"].split('@')[-1]
        if domain not in VALID_DOMAINS:
            raise ValidationError(
                'E-mail addresses from %(domain)s are not allowed.',
                code='invalid',
                params={'domain': domain},
            )
    
        return original_email