1 from django.forms import fields 2 from django.forms import Form 3 from django.forms import widgets
导入包之后,定义一个继承自Form的类并定义用于验证的字段:
1 class TestForm(Form): 2 user = fields.CharField(required=True, max_length=12, min_length=3, 3 error_messages={}, label='用户名:', initial='姓名', 4 widget=widgets.TextInput(attrs={'n':123}), 5 show_hidden_initial=False,validators=[])
age = fields.IntegerField(label='年龄:',max_value=12) email = fields.EmailField() img=fields.FileField() city=fields.TypedChoiceField(choices=[(1,'上海',),(2,'天津',),],initial=2) hobby=fields.MultipleChoiceField(choices=[(1,'羽毛球'),(2,'钢弹'),(3,'铁蛋')]) xxoo=fields.FilePathField(path='app1') fields.GenericIPAddressField(protocol='both',unpack_ipv4=False) xdb=fields.CharField(widget=widgets.Select(choices=[(1,'羽毛球'),(2,'钢弹'),(3,'铁蛋')])
Choicefield与CharField都可以生成下拉单选radio,参数choice格式为:列表里面包含元组,元组两个参数,数字(键)和字符串(值)