from django import forms from app01.models import UserInfo from django.forms import fields as Ffields from django.forms import widgets as Fwidgets import time, datetime class UserInfoModelForm(forms.ModelForm): age = Ffields.IntegerField( label='年龄', widget=Fwidgets.TextInput(attrs={'class': 'form-control'}), initial=22 ) gender = Ffields.CharField( label='性别', widget=Fwidgets.TextInput(attrs={'class': 'form-control'}), initial='男' ) birth = Ffields.DateField( label='生日', widget=Fwidgets.DateTimeInput() ) class Meta: # 与models建立了依赖关系......................................... model = UserInfo # 字段......................................................... fields = ['username', 'email', 'user_type', 'age'] # 排除字段...................................................... exclude = None, # 帮助提示信息................................................... help_texts = { 'username': '请输入账号', 'email': '请输入邮箱地址', 'user_type': '请选择客户类型' }, # 自定义插件..................................................... widgets = { 'username': Fwidgets.TextInput(attrs={'class': 'form-control'}) }, # 自定义字段类(也可以自定义字段)................................... field_classes = { # 设置此处,前端输入时,就必须是url,否则提示错误 # 'email': Ffields.URLField, } # 本地化,如:根据不同时区显示数据................................... localized_fields = ('ctime',) # 如果此处不写,会显示数据库模型设置的verbose_name.................... labels = { 'username': '账号', 'email': '邮箱', 'user_type': '类型' } # 自定义错误信息.................................................. # 整体错误信息from django.core.exceptions import NON_FIELD_ERRORS error_messages = { '__all__': { 'required': '此处不能为空' }, 'age': { 'invalid': '请输入一个有效的年龄' } }