class UserForm(forms.Form): username = fields.CharField( required=True,error_messages={'required':'用户名不能为空'}, widget=widgets.TextInput(attrs={'class':'form-control'}) ) #正则表达式 password = fields.CharField(required=True,error_messages={'required':'用户名不能为空'}, widget=widgets.TextInput(attrs={'class':'form-control'}) )#正则表达式 email = fields.EmailField(required=True,error_messages={'required':'用户名不能为空','invalid':'邮箱格式错误'}, widget=widgets.TextInput(attrs={'class': 'form-control'}) ) #正则表达式 # ut_id = fields.IntegerField( # required=True, # widget=widgets.Select(attrs={'class':'form-control'},choices=[(1,'普通用户'),(2,'超级用户')]) # ) ut_id = fields.IntegerField( required=True, widget=widgets.Select( attrs={'class':'form-control'}, choices=models.UserType.objects.values_list('id','name') ) ) ###解决数据库实时更新的问题 def __init__(self,*args,**kwargs): super(UserForm,self).__init__(*args,**kwargs) self.fields['ut_id'].widget.choices = models.UserType.objects.values_list('id','name')