具体可参考:http://www.liujiangblog.com/course/django/153

https://www.cnblogs.com/liuguniang/p/7141837.html

https://www.cnblogs.com/liwenzhou/p/8747872.html

一,Form字段:

1)验证

2)生成HTML(保留上次输入内容)

3)初始化默认值

 Form 重点字段:

------ChoiceField *****8

------MultipleChoiceField

------CharField

------IntegerField

------DecimalField

------DateField

------DateTimeField

------EmailField

------GenericIPAdressField--

------FileField

------RegexField

 

三,具体用法:

 1)CharField的参数

class TestForm(forms.Form):
user = fields.CharField(
required=True, # 是否必填
max_length=12, # 最大长度
min_length=3, # 最小长度
error_messages={ # 错误提示
'required':''
},
# widget = widgets.Textarea, # 定制html插件
# widget=widgets.Select,
label="用户名",
initial='请输入用户名', # 默认值
show_hidden_initial=False, # 是否在当前插件在家一个隐藏的且具有默认值的插件(可用于检验两次输入是否一致)
validators=[], # 自定制验证规则(是否是手机号)
localize=False, # 是否支持本地化
disabled=True, # 是否可以编辑
label_suffix=":",

)
age = fields.IntegerField(label="年龄",)
email = fields.EmailField(label="邮箱",)

 

当widget = widgets.Textarea 展示效果如下

 

当widget=widgets.Select展示效果如下

 

2)

简单了解
1){{ obj.as_p }}
2)
<ul>
  {{ obj.as_ul }}
</ul>
3)
<table>
{{ obj.as_table}}
</table>

 3)IntegerField类型 

age = fields.IntegerField(
			label="年龄",
			max_value=12,
			min_value=5,
			error_messages={
				'max_value' : "太大了"
			}
	)

 4)DecimalField类型

	weight = fields.DecimalField(
			label = "体重",
			max_value=30,  # 最大值
			min_value=10,  # 最小值
			max_digits=5,  #总长度
			decimal_places=3,  #小数位的长度
			
			
	)

 5)RegexField正则

6)EmailField

7)URLField

8)FileField

 

img = fields.FileField(label="上传文件")

 .html中

novalidate enctype="multipart/form-data"

 9)ChoiceField

	city = fields.ChoiceField(
			label="城市",
			choices=[(1,"上海"),(2,"北京"),(3,"天津"),],
			initial = 2,
			
	)

 10)MultipleChoiceField

hobby = fields.MultipleChoiceField(
			label="爱好",
			choices=[(1, "篮球"), (2, "足球"), (3, "乒乓球"), ],
			initial=[1, 2],  # 多选是列表
	)

 11)TypeChoiceField 转换类型的,打印输出的chocices 1不是字符串类型而是int类型

city = fields.TypedChoiceField(
			coerce=lambda x: int(x),#转换类型
			label="城市",
			choices=[(1, "上海"), (2, "北京"), (3, "天津"), ],
			initial=2,  # 单选是单值
	
	)

 

二.Form 之 插件:每一个fields字段都是一个正则表达式+默认的插件组成的,

1)更改默认插件:

widget = widgets.TextInput(attrs={'n':123}), #加自定义属性

 

 2)如果想把

txt = "<input type = 'text/>" 标签类的文本展示到页面上,需要在view传值的页面加上
from  django.utils.safestring import mark_safe

txt =mark_safe("<input type = 'text/>")

 3)Django 的内置插件: 单选的两种写法如下: select

只有select 有choice input什么的没有
xdb = fields.CharField(
			widget=widgets.Select(choices=[(1,"11"),(2,"22"),])
	)

也可以直接用ChoiceField
xdb = fields.ChoiceField( 
choices=[(1,"11"),(2,"22"),]
)

 多选的写法如下:

xdb = fileds.MultipleChoiceField(
  choices = [(1,"111"),(2,"222")]
  widget = widgets.SelectMultiple(attrs = {"class":"cl"})
)

 4)checkbox(单选的)

	xdb = fields.CharField(
			# widget=widgets.Select(choices=[(1,"11"),(2,"22"),])
			widget=widgets.CheckboxInput()
	)

 5)checkbox(多选的)

	xdb = fields.MultipleChoiceField(
			initial=[2,],
	        choices = [(1, "篮球"), (2, "足球"), (3, "乒乓球"), ],
			# widget=widgets.Select(choices=[(1,"11"),(2,"22"),])
			widget=widgets.CheckboxSelectMultiple()
	)

 6)radio

	xdb = fields.ChoiceField(
			initial=[2,],
	        choices = [(1, "篮球"), (2, "足球"), (3, "乒乓球"), ],
			# widget=widgets.Select(choices=[(1,"11"),(2,"22"),])
			widget=widgets.RadioSelect()
	)

 

 三.特殊的单选或者多选时,数据源是否能实时更新????/

 

 

 

 

 

 

 

 

 

 

 

 

 

https://www.cnblogs.com/

posted on 2018-05-16 18:20  wy0925  阅读(161)  评论(0编辑  收藏  举报