简体   繁体   English

Django Python 电子邮件验证在多个用户之间没有共享电子邮件

[英]Django Python email validations no shared email between multiple users

I am needing to know what code I need to put into my models.py (validations) so that when a user goes to register for a new account, that if they input a value into the email field that is already in the Data Base, that they get an error.我需要知道我需要将哪些代码放入我的 models.py(验证)中,以便当用户注册一个新帐户时,如果他们在数据库中已经存在的电子邮件字段中输入一个值,他们得到一个错误。

class UserManager(models.Manager): def user_registration_validator(self, post_data): errors = {} EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+ -]+@[a-zA-Z0-9. -]+.[a-zA-Z]+$')类 UserManager(models.Manager): def user_registration_validator(self, post_data): errors = {} EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+ -]+@[a-zA-Z0- 9. -]+.[a-zA-Z]+$')

    if len(post_data['first_name']) < 3:
        errors['first_name'] = "First name must be 3 characters"

    if post_data['first_name'].isalpha() == False:
        errors['first_name'] = "letters only"

    if len(post_data['last_name']) < 3:
        errors['last_name'] = "Last name must be 3 characters"

    if post_data['last_name'].isalpha() == False:
        errors['last_name'] = "letters only"

    if len(post_data['email']) < 8:
        errors['email'] = "Email must contain 8 characters"
    
    #if post_data['email'].Books.objects.filter(title=post_data) == True:
       # errors['email'] ="this email already exist in database"

    if post_data['email'].find("@") == -1:
        errors['email'] = "email must contain @ and .com"

    if post_data['email'].find(".com") == -1:
        errors['email'] = "email must contain @ and .com"
    
    # test whether a field matches the pattern
    if not EMAIL_REGEX.match(post_data['email']):
        errors['email'] = "Invalid email address!"

    if post_data['password'] != post_data['confirm_password']:
        errors['pass_match'] = "password must match confirm password"

    if len(post_data['password']) < 8:
        errors['pass_length'] = "password must be longer than 8 characters"

    return errors

在您的模型中添加一个如下所示的字段:

user_email = models.EmailField(unique=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM