forms组件
作者:互联网
forms组件
from django import forms
class MyForm(forms.Form):
name = forms.CharFiled(max_lenth =16,min_lengh =16,label='用户名')
password= forms.CharFiled(max_lenth =16,min_lengh =16,label='密码')
widget 改变标签样式
class LoginForm(forms.Form):
name = forms.CharField( required= True,
max_length=16,
min_length =6,
help_text='最少6位不能为空',
label='用户名',
strip=True,
error_messages={ 'required':'不能为空' }, widget=forms.widgets.TextInput() )
password = forms.CharField(max_length=16, min_length=6,
label='密码', error_messages={ 'required': '不能为空' }, widget=forms.widgets.PasswordInput(attrs={'class':'form-control'}) )
# sex = forms.ChoiceField(
# label='性别',
# initial=3,
# choices=((1,'男'),(2,'女'),(3,'保密')),
# widget = forms.widgets.RadioSelect() # # )
# city = forms.ChoiceField(
# label='城市',
# choices=((1,'北京'),(2,'天津'),(3,'上海')),
# initial=1,
# widget=forms.widgets.Select() # )
hobby = forms.MultipleChoiceField( #
label='爱好', #
choices=((1, '抽烟'), (2, '喝酒'), (3, '打麻将')), #
widget=forms.widgets.CheckboxSelectMultiple, # )
# grils = forms.MultipleChoiceField(
# label='女朋友',
# choices=((1, '抽烟'), (2, '喝酒'), (3, '打麻将')), #
widget=forms.widgets.SelectMultiple, # )
status = forms.ChoiceField( # choices=(('True',1),('False',0)), #
label = 'remember me!',
# widget= forms.widgets.CheckboxInput, # )
# birthday = forms.CharField( # label='生日', # widget=forms.widgets.TextInput(attrs={'type':'date'}) # )
第三方库和标准库之间得有换行
第三方库 import requests
标准库 from django ...
校验器
自义校验函数
def clean_name(self):
value = self.cleaned_data['name']
if '大壮' in value:
raise ValidationError('含有敏感词汇')
else:
return value
def clean(self):
#所有通过校验的数据
value = self.cleaned_data
p1 = value['password']
p2 = value['r_password']
if p1==p2:
return value
else:
self.add_error('r_password','两次输入密码不一致')
raise ValidationError('两次输入的密码不一致')
forms 插件 动态生成数据
publishs = forms.ModelChoiceField(
required=True,
label='出版社',
queryset= models.Publish.objects.all(),
error_messages={
'required': '不能为空',
},
widget=forms.widgets.Select()
)
authors = forms.ModelMultipleChoiceField(
label='作者',
queryset= models.Author.objects.all(),
error_messages = {
'required': '不能为空',
}, )
forms插件 init方法 添加属性
def __init__(self, *args, **kwargs):
super().__init__(*args,**kwargs)
#self.fields 是有序字典
for field in self.fields.values():
field.widget.attrs.update({'class':'form-control'})
iframe 嵌入别的网页
<iframe src="http://www.xiaohuar.com" frameborder="0" width="100%" height="200px"></iframe>
标签:widget,16,self,label,forms,widgets,组件 来源: https://www.cnblogs.com/learn-record/p/14661931.html