class FM(forms.Form): user = fields.CharField(error_messages={'required': '用户名不能为空'}, widget=widgets.Input(attrs={'class': 'c1'}), label="用户名") pwd = fields.CharField(max_length=12, min_length=6, error_messages={ 'required': '密码不能为空', 'max_length': '密码长度不能大于12位', 'min_length': '密码长度不能小于6位' }, widget=widgets.PasswordInput(attrs={'class': 'c2'}), label="密码") email = fields.EmailField(error_messages={ 'required': '邮箱不能为空', 'invalid': '邮箱格式错误' }, label="邮箱") f = fields.FileField(allow_empty_file=False) p = fields.FilePathField(path='app01') # 列出app01目录下所有文件 city1 = fields.ChoiceField(choices=[(0, '北京'), (1, '吉林'), (2, '银川')]) city2 = fields.MultipleChoiceField(choices=[(0, '广东'), (1, '深圳'), (2, '东莞')])
class FM(forms.Form): # 字段本身只做验证 user = fields.CharField( error_messages={'required': '用户名不能为空.'}, widget=widgets.Textarea(attrs={'class': 'c1'}), label="用户名", ) pwd = fields.CharField( max_length=12, min_length=6, error_messages={'required': '密码不能为空.', 'min_length': '密码长度不能小于6', "max_length": '密码长度不能大于12'}, widget=widgets.PasswordInput(attrs={'class': 'c2'}) ) email = fields.EmailField(error_messages={'required': '邮箱不能为空.','invalid':"邮箱格式错误"}) # f = fields.FileField() p = fields.FilePathField(path='app01') city1 = fields.ChoiceField( choices=[(0,'上海'),(1,'广州'),(2,'东莞')] ) city2 = fields.MultipleChoiceField( choices=[(0,'上海'),(1,'广州'),(2,'东莞')] )
class FM(forms.Form): # error_messages自定义错误显示信息。required/invalid/不能随便写,必须跟返回的obj.errors里面的code的值一样。如"code": "invalid",那么这里"invalid"就是key user = fields.CharField(error_messages={'required': '用户名不能为空'}, initial='sun', label='用户名', disabled=True) #widget用来引用插件,widgets.PasswordInput代表密码插件。 #这个插件有很多,input,select,radius,checkbox等都有。 # fields.CharField表示对字段验证,widget表示使用里面的某个插件 pwd = fields.SlugField(max_length=12, min_length=6, error_messages={ 'required': '密码不能为空', 'min_length': '太短了', 'max_length': '太长了', 'invalid': '只支持数字字母下划线减号' }, widget=widgets.PasswordInput) email = fields.EmailField(error_messages={ 'required': '用户名不能为空', 'invalid': '邮箱格式错误' }, initial='*****@*****.**') #可以定义input的类型,比如widgets.Textarea就是文本框,还可以定义属性,attrs可以定义里面的属性,样式等。 #引用文本框插件Textarea,并且给文本框定义样式。 msg = fields.CharField(required=False, error_messages={'required': '信息不能为空'}, widget=widgets.Textarea(attrs={ 'class': 'c1', 'name': 'msg' })) # phone=fields.CharField(validators=[RegexValdator(r'^[0-9]+$','请输入数字'),RegexValdator(r'^159[0-9]+$','数字必须以159开头')]) # 在页面上显示templates文件夹下面的所有文件 file = fields.FilePathField(path='templates') #下拉框 #单选框 c1 = fields.ChoiceField(choices=[(0, '北京'), (1, '上海'), (2, 'NewYork')], initial=2) #多选框 c2 = fields.MultipleChoiceField(choices=[(0, '北京'), (1, '上海'), (2, 'NewYork')], initial=[1, 2]) #单选按钮 c3 = fields.ChoiceField(choices=[(0, '北京'), (1, '上海'), (2, 'NewYork')], widget=widgets.RadioSelect) #单选select c4 = fields.CharField(widget=widgets.Select(choices=( (1, '上海'), (2, '北京'), ))) #单选勾选 c5 = fields.CharField(widget=widgets.CheckboxInput()) #多选checkbox c6 = fields.MultipleChoiceField(choices=( (1, '上海'), (2, '北京'), (3, '湖南'), ), widget=widgets.CheckboxSelectMultiple())
class FM(forms.Form): user = fields.CharField(widget=widgets.Textarea({"class":'c1'}), label="用户名", ) pwd = fields.CharField(max_length=12,min_length=6,error_messages={'min_length':'密码长度不能小于6','max_length':'密码长度不能大于12'}, widget=widgets.PasswordInput({"class":"c2"})) #可以自定义样式 email = fields.EmailField() p = fields.FilePathField(path='app') city1 = fields.ChoiceField( choices=[(0,'上海'),(1,'广州'),(2,'深圳')] ) city2 = fields.MultipleChoiceField( choices=[(0,'上海'),(1,'广州'),(2,'深圳')] )
class TestForm(forms.Form): user = fields.CharField( required=True, # 是否必填 max_length=12, # 最大长度 min_length=3, # 最小长度 error_messages={}, # 错误提示 widget = widgets.TextInput(attrs={'class':'c1'}), # 定制HTML插件 label='用户名', initial='请输入用户', help_text='asdfasdf', show_hidden_initial=False, # validators=[] disabled=True, label_suffix='->' ) age = fields.IntegerField( label='年龄', max_value= 12, min_value=5, error_messages={ 'max_value':'太大了' } ) email = fields.EmailField( label='邮箱' ) img = fields.FileField() city = fields.TypedChoiceField( coerce=lambda x: int(x), choices=[(1,'上海',),(2,'北京'),(3,'沙河'),], initial=2 ) bobby = fields.MultipleChoiceField( choices=[(1,'刚娘'),(2,'铁娘'),(3,'钢弹')], initial=[1,2] ) xoo = fields.FilePathField( path='app01' )
class FM(forms.Form): # user = forms.CharField() # pwd = forms.CharField() # email = forms.EmailField() # 字段本身只做认证 user = fields.CharField(error_messages={'required': "用户名不能为空"}, widget=widgets.Input(attrs={'id': 'username'}), label="用户名") pwd = fields.CharField( max_length=12, min_length=6, error_messages={ 'required': "密码不能为空", 'min_length': "密码长度不能小于6", 'max_length': "密码长度不能大于12" }, widget=widgets.PasswordInput(attrs={'id': 'pwd'}), # widget=widgets.PasswordInput, ) gender = fields.ChoiceField( choices=((1, '男'), (2, '女')), initial=2, label="性别:" # widget=widgets.RadioSelect # widget=widgets.Select ) # gender = fields.MultipleChoiceField( # choices=((1, '男'), (2, '女')), # initial=2, # label="性别:" # # widget=widgets.RadioSelect # ) email = fields.EmailField(error_messages={ 'required': "邮箱不能为空", 'invalid': "邮箱格式不对" }, widget=widgets.Input(attrs={'id': 'em'})) f = fields.FileField() f_show = fields.FilePathField(path='cache')
class FieldForm(DForms.Form): f1 = fields.CharField(max_length=6, required=True, initial="小虎", validators=[ RegexValidator(r'^[0-9]+$', '11111', code='f1'), RegexValidator(r'^159[0-9]+$', '2222', code='f2') ], error_messages={ 'required': '不能为空', 'f1': 'geshicuowu', 'f2': 'kajdlfkjasldf', 'max_length': 'taichangla' }, label='爱抚1', show_hidden_initial=True, disabled=False, label_suffix="-->") # f2 = fields.RegexField(r'^159[0-9]+$') f3 = fields.FileField() f4 = fields.ChoiceField(initial=2, choices=[( 1, '赵四', ), (2, "刘能"), (3, '六大脑袋')]) f5 = fields.TypedChoiceField(coerce=lambda x: int(x), initial=2, choices=[( 1, '赵四', ), (2, "刘能"), (3, '六大脑袋')]) f6 = fields.MultipleChoiceField(initial=[1, 2], choices=[( 1, '赵四', ), (2, "刘能"), (3, '六大脑袋')]) f7 = fields.SplitDateTimeField() f8 = fields.FilePathField(path='app01', allow_folders=True, recursive=True)
class FM(forms.Form): #第一种方式,最简版: # user = fields.CharField( # initial = 2, # widget = widgets.RadioSelect(choices=((1, '上海'), (2, '北京'),)) # ) user = fields.CharField( error_messages={'required': '用户名不能为空.'}, widget=widgets.TextInput( attrs={'class': 'c1'}), #定义插件,修改标签类型,CSS样式,属性等 label='用户名', # initial='root',#默认值 help_text='这是帮助信息', ) pwd = fields.CharField( max_length=12, min_length=6, error_messages={ 'required': '密码不能为空', 'min_length': '密码长度不能小于6', 'max_length': '密码长度不能大于12' }, widget=widgets.PasswordInput( attrs={'style': 'background-color:blue;color:white'})) email = fields.EmailField(error_messages={ 'required': '邮箱不能为空.', 'invalid': "邮箱格式错误" }) f = fields.FileField() p = fields.FilePathField(path='app1') city = fields.ChoiceField(choices=[ (0, '选项1'), (1, '选项2'), (2, '选项3'), ]) city1 = fields.MultipleChoiceField(choices=[ (0, '选项1'), (1, '选项2'), (2, '选项3'), ])