[Fixed]-Django unable to render form in table format

1👍

The form’s as_table method just uses <tr></td> instead of <div> to render your form – but it will be rendered visually the same way.

To easily get control over your form, consider using django-crispy-forms. Here is is how you would make your form render horizontally:

In your forms.py, add this (in addition to your normal code):

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class ExampleFormSetHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(ExampleFormSetHelper, self).__init__(*args, **kwargs)
        self.form_method = 'post'
        self.render_required_fields = True
        self.template = 'bootstrap/table_inline_formset.html'
        self.add_input(Submit("submit", "Save")) 

Next, in your views.py:

from .forms import ExampleFormsetHelper, YourFormSet

def formset_view(request):
    formset = YourFormSet()
    helper = ExampleFormSetHelper()
    return render(request, 'template.html',
                 {'formset': formset, 'helper': helper})

Finally, in your template, all you need is:

{% load crispy_forms_tags %}
{% crispy formset helper %}

For more details, the documentation has the specifics.

Leave a comment