首页 > 解决方案 > Django中的模型字段验证

问题描述

我想验证一个自定义表单域。我已经制作了联系我们表格的模型。字段是名称、主题、电子邮件和消息。现在“DIU”公司的用户只能联系我们。email 的电子邮件地址中必须包含“35-”和“@diu.edu.bd”。我怎样才能像这样验证这个电子邮件字段?请帮忙

标签: pythondjango

解决方案


在你的模型中这样的东西怎么样:

def clean_email(self):
    
    email = self.cleaned_data['email'].lower()
    
    if '@diu.edu.bd' not in email or '35-' not in email:
        raise forms.ValidationError(u'You are not authorized to send us email.')
    
    return email

推荐阅读