R
R
Rishat Sultanov2017-10-18 10:36:32
Django
Rishat Sultanov, 2017-10-18 10:36:32

How to use whitelist when validating a form?

Hi all!
Generally somehow so tried to make a call of the white list in the form. But I don't get it right.

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.forms import EmailField
from django.core.validators import EmailValidator


class SignupForm(UserCreationForm):
    email = forms.EmailField(validators=[EmailValidator(whitelist=['@iuca.kg'])])

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')

Help me please)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2017-10-18
@rishatss

Whitelist is needed for something else. If you need to check the domain name, then override clean_emails:

def clean_email(self):
    data = self.cleaned_data['email']
    if '@iuka.kg' not in data:
        raise forms.ValidationError('Неправильный email')
    return data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question