django的form的常用组件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的常用组件的主要内容,如果未能解决你的问题,请参考以下文章