使用crispy表单向表单字段添加属性

时间:2014-02-24 13:45:47

标签: python django django-crispy-forms

我正在尝试使用crispy表单向表单字段电子邮件添加属性

这就是我所拥有的

 def __init__(self, *args, **kwargs):
        super(RedeemForm, self).__init__(*args, **kwargs)
        setup_bootstrap_helpers(self)
        self.helper.layout = Layout(
            Fieldset(
                '',
                Div(
                    Div(
                        Field('mobile', css_class="form-control", title="test"),
                        css_class="col-xs-8",
                    ),
                    css_class="row",
                ),

                Div(
                    Div(
                        Field('email', css_class="form-control", title="test"),
                        css_class="col-xs-8",
                    ),
                    css_class="row",
                ),


            ),

        )


 mobile = forms.EmailField(max_length=100, required=True)
 email = forms.CharField(max_length=100, required=True)

我想添加required

 <input type="email" required />

我无法看到如何使用脆皮形式实现这一目标。

1 个答案:

答案 0 :(得分:4)

class RedeemForm(forms.ModelForm):
    email = forms.CharField(required=True)

    def __init__(self, *args, **kwargs):
        super(RedeemForm, self).__init__(*args, **kwargs)
        setup_bootstrap_helpers(self)
        .....................

        def __init__(self, *args, **kwargs):
            super(RedeemForm, self).__init__(*args, **kwargs)
            setup_bootstrap_helpers(self)
            .....................
            self.fields['email'].required = True