Displaying table rows with a form set in django-crispy-forms

I want to display a list of instances as formsetwith django-crispy-formsand bootstrap, where each instance is displayed as a string with all fields horizontally located.

All the examples I can find seem to display instances with fields vertically arranged.

I thought using:

helper.form_class = 'form-horizontal'

may work, but it has no effect.

+3
source share
2 answers

Use the template attribute in the helper (documented here ): helper.template = 'bootstrap/table_inline_formset.html'

+2
source

, , ,

:

from crispy_forms.helper import FormHelper, Layout
...

class MyForm(forms.ModelForm):

    class Meta:
        model = MyModel
        fields = ['field1', 'field2', 'field3']


MyFormSet = modelformset_factory(MyModel, form=MyForm, extra=0)


class MyFormSetHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(MyFormSetHelper, self).__init__(*args, **kwargs)
        self.layout = Layout(
            'field1',
            'field2',
            'field3'
        )
        self.template = 'bootstrap/table_inline_formset.html'

:

formset = MyFormSet(queryset=my_qs)
helper = MyFormSetHelper()
context = {'formset': formset, 'helper': helper}
return render(request, 'my_template.html', context)

:

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}

<form action="" method="post">
{% csrf_token %}
{% crispy formset helper %}
</form>

{% endblock content %}
0

All Articles