django的form的常用组件
发表于:2024-11-26 作者:热门IT资讯网编辑
编辑最后更新 2024年11月26日,class TestForm(Form):# t1 = fields.CharField(# widget=widgets.Textarea(attrs={})# )#
class TestForm(Form):# t1 = fields.CharField(# widget=widgets.Textarea(attrs={})# )### t2 = fields.CharField(# widget=widgets.CheckboxInput# )## t3 = fields.MultipleChoiceField(# choices=[(1,'篮球'),(2,'足球'),(3,'溜溜球')],# widget=widgets.CheckboxSelectMultiple# )## t4 = fields.ChoiceField(# choices=[(1,'篮球'),(2,'足球'),(3,'溜溜球')],# widget=widgets.RadioSelect# ) # t5 = fields.FileField( # widget=widgets.FileInput # ) # def clean_t1(self): # passfrom django.core.exceptions import ValidationErrorclass TestForm(Form): user = fields.CharField(validators=[]) pwd = fields.CharField() def clean_user(self): v = self.cleaned_data['user'] if models.Student.objects.filter(name=v).count(): raise ValidationError('用户名已经存在') return self.cleaned_data['user'] def clean_pwd(self): return self.cleaned_data['pwd'] def clean(self): # user = self.cleaned_data.get('user') # email = self.cleaned_data.get('email') # if models.Student.objects.filter(user=user,email=email).count(): # raise ValidationError('用户名和邮箱联合已经存在') return self.cleaned_data # def _post_clean(self): # """ # An internal hook for performing additional cleaning after form cleaning # is complete. Used for model validation in model forms. # """ # passdef test(request): obj = TestForm(initial={'t3':[2,3]}) obj.is_valid() return render(request,'test.html',{'obj':obj})