单选按钮组
grade=forms.ChoiceField(label='grade',required=True,widget=forms.RadioSelect,choices=GRADE_CHOICES)
下拉列表框
grade=forms.ChoiceField(label='grade',required=True,widget=forms.Select,choices=GRADE_CHOICES)
复选按钮组
grade=forms.MultipleChoiceField(label='grade',required=True,widget=forms.CheckboxSelectMultiple(),choices=GRADE_CHOICES)
单复选框
CheckboxInput
文本域
message = forms.CharField(label='留言内容',widget=forms.Textarea)
可多选的下拉列表框,,功能强大,占地大不太好看
SelectMultiple
select 下拉列表框,单选 <p> {% for radio in form.grade %} <div class="form-group"> {{ radio }} </div> {% endfor %} </p> 文本域 message = forms.CharField(label='留言内容',widget=forms.Textarea) 表单和model联动,单选下拉对象manager,多选对象databases,上传文件attachment class CreatetaskForm(forms.Form): creater = forms.CharField(label=u"创建者",widget=BootstrapUneditableInput()) manager = forms.ModelChoiceField(queryset=Manager.objects.all(),required=True,label=u"项目负责人",error_messages={'required': u'必选项'},) databases = forms.ModelMultipleChoiceField(queryset=Database.objects.order_by('id'),required=True,label=u"数据库",error_messages={'required': u'至少选择一个'},widget=forms.CheckboxSelectMultiple,) sql = forms.CharField(required=False,label=u"执行SQL",widget=forms.Textarea(attrs={'placeholder':"请在表名前加上schema,如hospital要写成p95169.hospital",'rows':5,'style':"100%",}),) desc = forms.CharField(required=False,label=u"描述",widget=forms.Textarea(attrs={'placeholder':"如果不是执行SQL(如数据的导入导出等),一定要在描述里说清楚",'rows':5,'style':"100%",}),) attachment = forms.FileField(required=False,label=u"附件",help_text=u"如果SQL文本过长,超过2000个字符,请上传附件") def clean(self): if not self.is_valid(): raise forms.ValidationError(u"以下红色标记部分为必选项") elif self.cleaned_data['sql'] == u'' and self.cleaned_data['desc'] == u'' : raise forms.ValidationError(u"如果执行SQL为空,描述为必填项") else: cleaned_data = super(CreatetaskForm, self).clean() return cleaned_data # 单radio,值为字符串 user = fields.CharField(initial=2,widget=widgets.RadioSelect(choices=((1,'上海'),(2,'北京'),))) # 单radio,值为字符串 user = fields.ChoiceField(choices=((1, '上海'), (2, '北京'),),initial=2,widget=widgets.RadioSelect) # 单select,值为字符串 user = fields.CharField(initial=2,widget=widgets.Select(choices=((1,'上海'),(2,'北京'),))) # 单select,值为字符串 user = fields.ChoiceField(choices=((1, '上海'), (2, '北京'),),initial=2,widget=widgets.Select) # 多选select,值为列表 user = fields.MultipleChoiceField(choices=((1,'上海'),(2,'北京'),),initial=[1,],widget=widgets.SelectMultiple) # 单checkbox user = fields.CharField(widget=widgets.CheckboxInput()) # 多选checkbox,值为列表 user = fields.MultipleChoiceField(initial=[2, ],choices=((1, '上海'), (2, '北京'),),widget=widgets.CheckboxSelectMultiple