[Answered ]-Single CrispyForm Tabs 2 Django Models

2👍

If you want put all tabs to one holder – simply create “TabHolder” markup manually (copy paste autogenereted one from browser) in your template and in your forms.py you can split your form to several helpers:

class CommonFormHelper(FormHelper):
    def __init__(self):
        super(CommonFormHelper, self).__init__()
        self.disable_csrf = True
        self.form_tag = False


class TestCrispyForm(forms.ModelForm):
    class Meta:
        model = PtData

    def __init__(self, *args, **kwargs):
        self.helper_name_info = CommonFormHelper()
        self.helper_name_info.layout = Layout(
            Tab(
                'Name',
                'firstname', 'lastname',
            ),
        )

        self.helper_location_info = CommonFormHelper()
        self.helper_location_info.layout = Layout(
            Tab(
                'Address',
                'address', 'city', 'state',
            ),
        )

        self.helper_personal_info = CommonFormHelper()
        self.helper_personal_info.layout = Layout(
            Tab(
                'Miscellaneous',
                'sex', 'age', 'birthday', 'email',
            ),
        )
        super(TestCrispyForm1, self).__init__(*args, **kwargs)


class TestCrispyForm2(forms.ModelForm):
    class Meta:
        model = PtHx

    def __init__(self, *args, **kwargs):
        self.helper_some_info = CommonFormHelper()
        self.helper_some_info.layout = Layout(
            Tab(
                'Tab 3',
                InlineRadios('HypertensionHX'),
            ),
        )

        self.helper_some_other_info = CommonFormHelper()
        self.helper_some_other_info.layout = Layout(
            Tab(
                'Tab 4',
                'some_other_field_or_whatevere_you_want_here',
            ),
        )
        super(TestCrispyForm2, self).__init__(*args, **kwargs)

template:

{# ... #}
<form method="post" enctype="multipart/form-data" class="mainForm">{% csrf_token %}
     {# paste begin of TabHolder markup #}
         {# of course you can change ordering =) #}

         {% crispy form form.helper_name_info %}
         {% crispy form form.helper_location_info %}
         {% crispy form form.helper_personal_info %}
         {% crispy form2 form2.helper_some_info %}
         {% crispy form2 form2.helper_some_other_info %}
     {# paste end of TabHolder markup #}

     <input type="submit" value="Submit">
</form>

Leave a comment