1. 程式人生 > >django的form的常用組件

django的form的常用組件

組件

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): # pass from django.core.exceptions import ValidationError class 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. # """ # pass def test(request): obj = TestForm(initial={‘t3‘:[2,3]}) obj.is_valid() return render(request,‘test.html‘,{‘obj‘:obj})

django的form的常用組件