user2086641
user2086641

Reputation: 4371

Custom formset templates in Django

I am using a Django formset for this model:

class Book(models.Model):
    book_id=models.AutoField(primary_key=True,unique=True)
    book_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    author=models.ForeignKey(Author)

The formset is defined thus:

BookFormset = inlineformset_factory(Author, Book, 
    fields=('book_id','book_name', 'publisher_name'), extra=1, 
    can_delete=False)

The template is:

{{ formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{% for form in formset.forms %}
  {% if forloop.first %}
  <thead><tr>
    {% for field in form.visible_fields %}
    <th>{{ field.label|capfirst }}
    {% endfor %}
  </tr></thead>
  {% endif %}
  <tr class="{% cycle row1,row2 %}">
  {% for field in form.visible_fields %}
    <td>
    {# Include the hidden fields in the form #}
    {% if forloop.first %}
      {% for hidden in form.hidden_fields %}
      {{ hidden }}
      {% endfor %}
    {% endif %}
      {{ field.errors.as_ul }}
      {{ field }}
    </td>
  {% endfor %}
  </tr>
{% endfor %}    
</table>

The fields are displayed column-wise, but I would like them to be displayed row-wise.

The above code produces output like this:

Book name   Publisher name

book_field       Publisher_field

I would like the output to look like this:

Book name   book_field

Publisher name   Publisher_field

How can I do this?

Upvotes: 3

Views: 6026

Answers (2)

David Stockwell
David Stockwell

Reputation: 21

The examples above seem to show a column-wise layout, which appears to be the default layout when a formset renders itself.

To make it row-wise, use something like this:

<table>
    {% for form in formset.forms %}
        {% if forloop.first %}
            <thead>
                {% for field in form.visible_fields %}
                    <th>{{ field.label }}</th>
                {% endfor %}    
            </thead>
            <tbody>
        {% endif %}

        <tr class="{% cycle row1,row2 %}">                        
            {% for field in form.visible_fields %}
                <td>
                    {% if forloop.first %}
                        {% for hidden in form.hidden_fields %}
                            {{ hidden }}
                        {% endfor %}
                    {% endif %}

                    {{ field.errors.as_ul }}
                    {{ field }}
                </td>
            {% endfor %}
        </tr>
        {% if forloop.last %}
            </tbody>
        {% endif %}
    {% endfor %}    
</table>

Upvotes: 2

Gareth Rees
Gareth Rees

Reputation: 65854

In your template, you have two <tr> elements, each of which contains a loop over form.visible_fields, each iteration of which generates a single <th> or <td>.

Change this round so that you have a single loop over form.visible_fields, each iteration of which contains a single <tr> element containing a <th> and a <td>. Like this:

<table id="formset" class="form">
{% for form in formset.forms %}
  {% for field in form.visible_fields %}
  <tr class="{% cycle row1,row2 %}">
    <th>{{ field.label|capfirst }}</th>
    <td>
    {# Include the hidden fields in the form #}
    {% if forloop.first %}
      {% for hidden in form.hidden_fields %}
      {{ hidden }}
      {% endfor %}
    {% endif %}
      {{ field.errors.as_ul }}
      {{ field }}
    </td>
  </tr>
  {% endfor %}
{% endfor %}    
</table>

Upvotes: 2

Related Questions