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
    
    
     
  • 相关阅读:
    微信开发-Jssdk调用分享实例
    软件工程
    Homework_4 四则运算
    Homework 3
    每周总结
    自动生成四则运算题目
    Homework 1 -- The beginning
    程序员视角的餐饮行业
    iOS9网络适配_ATS:改用更安全的HTTPS
    Xcode的 发展史
  • 原文地址:https://www.cnblogs.com/zxmbky/p/9707582.html
Copyright © 2011-2022 走看看