在Django Crispy表单中指定From的模板

时间:2015-03-26 17:36:06

标签: django django-forms django-crispy-forms

我有一个脆弱的表单,我想在其中呈现一个单独的表单。我能够渲染第二种形式,但需要设计风格。我正在尝试为这个单独的表单(FacilityForm)指定一个模板。我怎样才能做到这一点? 我根据用户是否点击“添加新设施”来显示/隐藏这个单独的“设施表单”。

我的脆皮形式:

class RegistrationForm(forms.Form):
    # Fields- many more fields that I haven't listed here
    sample = forms.CharField(
        label="Sample",
        max_length = 50,
        required = True,
    )

    data_type = forms.ChoiceField(
        label="Data Type",
        required = True,
    )

     def __init__(self, *args, **kwargs):
        super(RegistrationForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Fieldset(
                "Data Information",
                 Div(
                     Div('lab', css_class='col-xs-6'),
                     Div('sample', css_class='col-xs-6'),
                     css_class='row'
            ),

            Div(
                Div('facility', css_class='col-xs-6'),
                Div( HTML("<a href='#'>Add a new facility</a>"), css_class='col-xs-6 addfacility'),
                css_class='row',
            ),
            Div(
                HTML("'{{facility_form}}' <a href='#' id='cancelFacility'>cancel</a>"),
                css_class='new_facility',
            ),

        ),

单独表格:

class FacilityForm(forms.Form):

    facility_name = forms.CharField(
        max_length=256,
        required=True,
    )

    address_line1 = forms.CharField(
        max_length=256,
        required=True,
    )

    country = forms.CharField(
        max_length=50, 
        required=True,
    )

    state_province = forms.CharField(
        max_length=100, 
        required=False,
    )

1 个答案:

答案 0 :(得分:0)

我想这可能是多余的,但我在外部Div中使用了很多Div,css_class我设置为new_facility

            Div(
                Fieldset('Facility Information',
                    Div(
                        Div('facility_name', css_class='col-xs-4'),
                        Div('address_line1', css_class='col-xs-4'),
                        Div('address_line2', css_class='col-xs-4'),
                        css_class='row',
                    ),
                    Div(
                        Div('country', css_class='col-xs-3'),
                        Div('state_province', css_class='col-xs-3'),
                        Div('city', css_class='col-xs-3'),
                        Div('postal_code', css_class='col-xs-3'),
                        css_class='row',
                    ),
                ),
                Fieldset('Facility Contact Person',
                    Div(
                        Div('first_name', css_class='col-xs-4'),
                        Div('middle_initial', css_class='col-xs-4'),
                        Div('last_name', css_class='col-xs-4'),
                        css_class='row',
                    ),
                    Div(
                        Div('email', css_class='col-xs-12 col-sm-6'),
                        css_class='row',
                    ),
                ),

                HTML("<a href='#' class='btn btn-default pull-right' id='cancelFacility'>Cancel</a>"),


                css_class='new_facility',
        ),