记录自己的bug

can only concatenate str (not "NoneType") to str

# django中 忘记对密码属性写lable 
# 要注意这样写的话,每一个字段都要有lable属性,否则会报这个错误
# can only concatenate str (not "NoneType") to str  
# TypeError:只能将str(不是“NoneType”)连接到str

class 用户注册对象(forms.ModelForm):
    # 重新对数据库字段格式写校验的规则
    手机号 = forms.CharField(label="手机号",validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$','手机号输入错误'),])
    密码 = forms.CharField(widget=forms.PasswordInput(),label="密码")    #重新修改数据库的插件为密码
    重复密码 = forms.CharField(widget=forms.PasswordInput(),label="重复密码")
    验证码 =forms.CharField(label="验证码")
    class Meta:
        model = models.用户表
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs = {"class": "form-control", "placeholder": "请输入" + field.label}
            # field.widget.attrs['class'] = "form-control"  仅仅加一个属性的写法  要注意这样写的话,每一个字段都要有lable属性
            #can only concatenate str (not "NoneType") to str  否则会报这个错误  TypeError:只能将str(不是“NoneType”)连接到str
posted @ 2022-05-06 01:10  铁铁匠  阅读(134)  评论(0)    收藏  举报