from django.forms import widgets
class UserForm(forms.Form): user=forms.CharField(min_length=5,label="用戶名") pwd=forms.CharField(min_length=5,widget=widgets.PasswordInput(),label="密碼") r_pwd=forms.CharField(min_length=5,widget=widgets.PasswordInput(),label="確認密碼") email=forms.EmailField(min_length=5,label="郵箱") # def __init__(self, *args, **kwargs): # super().__init__(*args, **kwargs) # for filed in self.fields.values(): # filed.widget.attrs.update({'class': 'form-control'})
def clean_user(self): val=self.cleaned_data.get("user") user=UserInfo.objects.filter(username=val).first() if user: raise ValidationError("用戶已存在!") else: return val def clean_pwd(self): val=self.cleaned_data.get("pwd") if val.isdigit(): raise ValidationError("密碼不能是純數字!") else: return val def clean_email(self): val=self.cleaned_data.get("email") if re.search("\w+@163.com$",val): return val else: raise ValidationError("郵箱必須是163郵箱!") def clean(self): pwd=self.cleaned_data.get("pwd") r_pwd=self.cleaned_data.get("r_pwd") if pwd and r_pwd and r_pwd!=pwd: self.add_error("r_pwd", ValidationError("兩次密碼不一致!")) else: return self.cleaned_data
#### widget=widgets.TextInput(attrs={'class': 'form-control'})