django的form的常用组件

发布时间:2020-04-07 21:04:30 作者:kesungang
来源:网络 阅读:546
 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})
推荐阅读:
  1. Django Form和ModelForm组件
  2. Django form 实现文件上传

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

组件 常用组件 fo

上一篇:十一、流程控制之goto语句

下一篇:PHP依赖的lib库文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》