from django import forms from django.core.exceptions import ValidationError from models import * def age_validate(age): if age <= 0: raise ValidationError('年龄错误') elif age > 150: raise ValidationError('老妖精') # 用户信息表单 class UserInfoForm(forms.Form): STATUS = ((None, '请选择'), (0, '正常'), (1, '无效')) username = forms.CharField(label="用户名", min_length=6, widget=forms.widgets.TextInput( attrs={'class': 'form-control', 'placeholder': '请输入用户名'})) password = forms.CharField(label='密码', min_length=6, max_length=10, widget=forms.widgets.PasswordInput(attrs={'class': 'password'}, render_value=True), help_text='密码长度为6-10个字符') age = forms.IntegerField(label='年龄', initial=1, min_value=1, max_value=150, validators=[age_validate]) mobile = forms.CharField(label='手机号码', min_length=11, max_length=11) status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select) # status = forms.ModelChoiceField(queryset=UserStatus.objects.all(), empty_label='请选择') createdate = forms.DateField(label='创建日期', required=False, input_formats=["%Y-%m-%d"])