本文共 5245 字,大约阅读时间需要 17 分钟。
from django.shortcuts import render, HttpResponsefrom django import formsclass LoginForm(forms.Form): name = forms.CharField( label='用户名', initial='陌生人', strip=True, error_messages={ 'required': '用户名不能为空', } ) password = forms.CharField( label='密码', min_length=6, widget=forms.PasswordInput(render_value=True), error_messages={ 'required': '密码不能为空', 'min_length': '密码不能小于6位', } ) gender = forms.ChoiceField( choices=[(1, '男'), (2, '女'), (3, '保密')], label='性别', initial=3, widget=forms.RadioSelect(), error_messages={ 'required': '请选择性别', } ) hobby = forms.ChoiceField( label='爱好', widget=forms.Select(), choices=((1, '篮球'), (2, '足球'), (3, '乒乓球')), initial=2, ) hobby2 = forms.MultipleChoiceField( label='爱好2', choices=((1, '摩托车'), (2, '汽车'), (3, '游艇')), initial=[1, 3], widget=forms.SelectMultiple(), ) keep = forms.ChoiceField( label='是否记住密码', initial='checked', widget=forms.CheckboxInput(), ) city = forms.ChoiceField( label='居住城市', choices=[(1, '北京'), (2, '天津'), (3, '上海'), (4, '武汉')], initial=4, widget=forms.Select(), )def login(request): form_obj = LoginForm() if request.method == 'POST': form_obj = LoginForm(request.POST) if form_obj.is_valid(): pass return render(request, 'app/login.html', {'form_obj': form_obj}) Field的常用属性包括:
required=True:字段是否必填,默认为真widget=None:绑定特定的HTML插件label=None:用于生成Label标签或显示内容initial=None:字段初始值help_text='':帮助信息(显示在标签旁边)error_messages=None:定义错误信息validators=[]:自定义验证规则localize=False:是否支持本地化disabled=False:是否可用label_suffix=None:Label内容后缀如需扩展,其它字段可根据需求添加,例如:
CharField:最大长度、最小长度、是否移除空白IntegerField:最大值、最小值RegexField:自定义正则表达式ChoiceField:枚举类型选择RegexValidator**验证器:from django.core.validators import RegexValidatorno = forms.CharField( label='员工编号', validators=[ RegexValidator(r'^[0-9]+', '请输入数字'), RegexValidator('^110[0-9]+$', '请以110开头') ]) import redef mobile_validate(value): mobile_re = re.compile(r'^1[2356789]{1}[0-9]{9}$') if not mobile_re.match(value): raise ValidationError('手机号格式错误')class LoginForm(forms.Form): mobile = forms.CharField( label='手机号', validators=[mobile_validate, ], error_messages={ 'required': '手机号不能为空', } ) class LoginForm(forms.Form): description = forms.CharField( label='内容描述', initial='暂无描述', min_length=4, error_messages={ 'required': '不能为空', 'invalid': '格式错误', 'min_length': '最少评论4个字' } ) def clean_description(self): value = self.cleaned_data.get('description') if '666' in value: raise ValidationError('请不要喊666') else: return value class LoginForm(forms.Form): password = forms.CharField( label='密码', min_length=6, widget=forms.PasswordInput(), error_messages={ 'required': '密码不能为空', 'min_length': '密码不能小于6位' } ) repassword = forms.CharField( label='请再次输入密码', min_length=6, widget=forms.PasswordInput(), error_messages={ 'required': '密码不能为空', 'min_length': '密码不能小于6位' } ) def clean(self): password_value = self.cleaned_data.get('password') repassword_value = self.cleaned_data.get('repassword') if password_value == repassword_value: return self.cleaned_data else: self.add_error('repassword', '两次密码不一致') 转载地址:http://vwhxz.baihongyu.com/