zoukankan      html  css  js  c++  java
  • 关于django form验证是否用户名已存在

    想通过django的Form模块进行数据库中是否已存在用户名的验证,首先我先调用了数据库用户名字段所有的值,发现是个queryset对象。

    随后经过查询后发现queryset查询集对象可以调用list工厂方法后,生成一个类似于[('user_name_1','username_2')]的对象,随即把列表中的元组对象拿出来进行遍历,完成验证,form代码如下:

    class Registerform(forms.Form):
        user_name = forms.CharField(label='你的姓名', max_length=10,error_messages={
            
        })
        user_psd = forms.CharField(label='你的密码',widget=forms.PasswordInput())
        user_psd2 = forms.CharField(label='请再次输入你的密码',widget=forms.PasswordInput())
        email = forms.EmailField(label='你的邮件地址',widget=forms.TextInput)
        def clean(self):
            cleaned_data = self.cleaned_data
            pwd = self.cleaned_data['user_psd']
            pwd2 =self.cleaned_data['user_psd2']
            user_form = self.cleaned_data['user_name']
            user_model = list(models.User_info.objects.all().values_list('user_name'))
            for i in user_model:#此步i为类似(username_1,username_2,)的元组对象 
                print(user_form)
                if user_form in i:
                    raise forms.ValidationError('用户名已存在,请重新尝试登录')
            if pwd!=pwd2:
                raise forms.ValidationError('两次输入的密码不匹配')
            return cleaned_data
    
    
     
  • 相关阅读:
    Rancher安装
    JDK8日期时间对象
    String经典面试题
    String
    单例模式
    多线程
    接口
    代码块
    内存吞金兽(Elasticsearch)的那些事儿 -- 常见问题痛点及解决方案
    内存吞金兽(Elasticsearch)的那些事儿 -- 写入&检索原理
  • 原文地址:https://www.cnblogs.com/zxmbky/p/9707582.html
Copyright © 2011-2022 走看看