【发布时间】:2012-08-10 00:30:02
【问题描述】:
给定一个电子邮件地址(例如:goelv@example.com),我如何验证该域(“example.com”)是否包含在给定的域列表中。如果域(“example.com”)不在指定列表中,表单应该会引发某种错误。
这是我目前在 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
使用此代码,我收到错误“'type' object has no attribute 'getitem'”,该错误可追溯到我的“raise forms.ValidationError[...]”行代码。
谁能看到我做错了什么?感谢您的帮助!
【问题讨论】:
标签: python django django-forms validation django-validation