标签:obb 数位 disable one def 允许 保密 ror message
Field
required=True, 是否允许为空
widget=None, HTML插件
label=None, 用于生成Label标签或显示内容
initial=None, 初始值
help_text='', 帮助信息(在标签旁边显示)
error_messages=None, 错误信息 {'required': '不能为空', 'invalid': '格式错误'}
validators=[], 自定义验证规则
localize=False, 是否支持本地化
disabled=False, 是否可以编辑
label_suffix=None Label内容后缀
CharField(Field)
max_length=None, 最大长度
min_length=None, 最小长度
strip=True 是否移除用户输入空白
IntegerField(Field)
max_value=None, 最大值
min_value=None, 最小值
DecimalField(IntegerField)
max_value=None, 最大值
min_value=None, 最小值
max_digits=None, 总长度
decimal_places=None, 小数位长度
<body>
<form action="" method="post" novalidate> <-! 加novalidate前端就不会提示-></-!>
{% csrf_token %}
{% for foo in form_obj %}
<p>{{ foo.label }}:{{ foo }} <span style="color: red">{{ foo.errors.0 }}</span></p>
{% endfor %}
<input type="submit" value="登录">
</form>
</body>
class MyForm(forms.Form):
username = forms.CharField(min_length=3,max_length=8,label='用户名',error_messages={
'min_length':'用户名最少为3位',
'max_length':'用户名最多为8位', #(给错误信息修改为中文)
'required':'用户名不能为空'
},widget=widgets.TextInput(attrs={'class':'form-control'}))
password = forms.CharField(min_length=3,max_length=8,label='密码',error_messages={
'min_length':'密码最少为3位',
'max_length':'密码最多为8位',
'required':'密码不能为空'
},widget=widgets.PasswordInput(attrs={'class':'form-control'}))
#给input标签增加type="password"&class="form-control"
re_password = forms.CharField(label='确认密码',error_messages={'required':'确认密码不能为空'},widget=widgets.PasswordInput(attrs={'class':'form-control'}))
email = forms.EmailField(label='邮箱', error_messages={
'required': '邮箱不能为空',
'invalid': '邮箱格式不正确' #邮箱格式修改中文关键字为invalid
}, required=False,widget=widgets.EmailInput(attrs={'class':'form-control'}))
#required=False为设置该Input框允许为空
#全局钩子
def clean(self):
password = self.cleaned_data.get('password')
re_password = self.cleaned_data.get('re_password')
if password != re_password:
self.add_error('re_password','两次输入密码不一致')
return self.cleaned_data ###可以不加
#局部钩子
def clean_username(self):
username = self.cleaned_data.get('username')
if '#'or 'or' in username:
self.add_error('username','用户名不能为#或or')
return username
def formm(request):
form_obj = MyForm()
if request.method == 'POST':
form_obj = MyForm(request.POST)
if form_obj.is_valid():
return HttpResponse('牛逼')
return render(request,'register.html',locals())
gender = forms.fields.ChoiceField(
choices=((1, "男"), (2, "女"), (3, "保密")),
label="性别",
initial=3,
widget=forms.widgets.RadioSelect()
)
hobby = forms.ChoiceField(
choices=((1, "篮球"), (2, "足球"), (3, "双色球"), ),
label="爱好",
initial=3,
widget=forms.widgets.Select()
)
keep = forms.ChoiceField(
label="是否记住密码",
initial="checked",
widget=forms.widgets.CheckboxInput()
)
hobby = forms.MultipleChoiceField(
choices=((1, "篮球"), (2, "足球"), (3, "双色球"), ),
label="爱好",
initial=[1, 3],
widget=forms.widgets.SelectMultiple()
)
hobby = forms.MultipleChoiceField(
choices=((1, "篮球"), (2, "足球"), (3, "双色球"),),
label="爱好",
initial=[1, 3],
widget=forms.widgets.CheckboxSelectMultiple()
)
标签:obb 数位 disable one def 允许 保密 ror message
原文地址:https://www.cnblogs.com/qwksjy/p/11760829.html