[Fixed]-Forms not rendering correctly in Django

1👍

You can see here Outputting forms as HTML that when you directly print a form instance the default output is a <table>

But when you render the form inside a template using {{ form }}, then these table tags are omitted.

0👍

As mentioned by Kartik Anand, this is just default templating of django form and there are more of them.

But if you’re not satisfied with default, there is way to print it in your template as you like. Django form is an iterable and when iterating through, it will give you each field from form one by one. Each field will be of class BoundField that will allow you to get full tags for label, field and errors list.

So if you want to build plain form, you can do:

for bound_field in form:
    print(bound_field.label_tag())
    print(bound_field)
    print(bound_field.errors)

Leave a comment