Search code examples
pythondjangodjango-crispy-forms

Dynamic Tab names in Tabholder in Django-crispy-forms


I wondering is there any chance to use "dynamic" names for Tabs.

Like in next example Tab names are hardcoded 'First Tab' and 'Second Tab' in ModelForm. I have dynamic titles (head1, 2 etc.) for every tabs and I am using values from database for titles. I like to use same text dynamically in tab names.

    Tab('First Tab',
        HTML("""<h4>{{ head1.0 }}</h4>"""),
        'field_name_1',
        Div('field_name_2')
    ),
    Tab('Second Tab',
        HTML("""<h4>{{ head2.0 }}</h4>"""),
        Field('field_name_3', css_class="extra")
    )

I would like to add same dynamic title texts to tab names also like in next example, but I have not found solutions.

    Tab({{ head1.0 }},
        HTML("""<h4>{{ head1.0 }}</h4>"""),
        'field_name_1',
        Div('field_name_2')
    ),
    Tab({{ head2.0 }},
        HTML("""<h4>{{ head2.0 }}</h4>"""),
        Field('field_name_3', css_class="extra")
    )

Solution

  • Can you assign the value from the database to a variable and pass it in to the form?

    title = value_from_database
        Tab(title,
            HTML("""<h4>{{ head1.0 }}</h4>"""),
            'field_name_1',
            Div('field_name_2')
        ),