Cho một địa chỉ email (ví dụ: [email protected]), làm cách nào để xác thực rằng tên miền ("example.com") được bao gồm trong một danh sách tên miền nhất định. Nếu tên miền ("example.com") không có trong danh sách được chỉ định, biểu mẫu sẽ tăng một số lỗi.Làm cách nào để xác thực tên miền của địa chỉ email dưới dạng?
Đây là những gì tôi có cho đến nay trong forms.py
class UserCreationFormExtended(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2",)
def clean_email(self):
data = self.cleaned_data['email']
domain = data.split('@')[1]
domain_list = ["gmail.com", "yahoo.com", "hotmail.com",]
if domain not in domain_list:
raise forms.ValidationError["Please enter an Email Address with a valid domain"]
return data
def save(self, commit=True):
user = super(UserCreationFormExtended, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
Với mã này, tôi nhận được lỗi " 'loại' đối tượng không có thuộc tính 'GetItem'" mà dấu vết đến dòng "raise forms.ValidationError [...]" trong mã của tôi.
Có ai có thể xem tôi đang làm gì sai không? Cảm ơn đã giúp đỡ!
wow , Tôi là một thằng ngốc. Cảm ơn! – goelv
@goelv: Vui lòng chấp nhận câu trả lời. – RanRag