Home > Back-end >  Django Form as a Table
Django Form as a Table

Time:12-14

The following code in the django template language is working but i don't know why. The purpose of this code is to display a form as a table with a number of columns. The first thing that throws me of is that the tag for opening a row is never giving but it is still made.

{% extends 'base.html' %}
{% load render_table from django_tables2 %}
{% block content %}
<form method="get">
    {% csrf_token %}
    <table>
        <tbody>
            {% for field in filter.form %}
    <td>{{ field.label}}</td>
    <td>{{ field }}</td>
                {% if forloop.counter|divisibleby:"4"%}
    </tr>
                {% endif %}
            {% endfor %}
        </tbody>
    </table>
    <input type="submit" value="Search">
</form>
{% render_table table%}
{% endblock %}

This generates a four column table. Is there any way the opening tags can be explicitly declared?And why is this code working?

enter image description here

I have tried to explicitly create the tags for row explicitly but this didn't not create the table correctly.It had an empty row space and a extra row.

{% extends 'base.html' %}
{% load render_table from django_tables2 %}
{% block content %}

<form method="get">
    {% csrf_token %}
    <table>
        <tbody>
            {% for field in filter.form %}
                {% if forloop.counter|divisibleby:"5"%}
    <tr>
                {% endif %}
                {% if forloop.counter|divisibleby:"5" == False %}
    <td>{{ field.label}}</td>
    <td>{{ field }}</td>
                {% endif %}
                {% if forloop.counter|divisibleby:"5"%}
    </tr>
                {% endif %}
            {% endfor %}
        </tbody>
    </table>
    <input type="submit" value="Buscar">
</form>

{% render_table table%}
{% endblock %}

CodePudding user response:

Why is this working? HTML is pretty lax and browsers can infer what you're trying to do with the context. td before a tr, there should be a row before -> add one

any way the opening tags can be explicitly declared? I believe with a combination of what you have and {% forloop.first %} {% forloop.last %} this can be done. Something along the lines of:

<table>
  <tbody>
  {% for field in filter.form %}

    {% if forloop.first %}  
      <tr>
    {% endif %}

    <td>{{ field.label}}</td>
    <td>{{ field }}</td>

    {% if forloop.last %}  
      <tr>
    {% elif forloop.counter|divisibleby:"4"%}
      </tr>
      <tr>
    {% endif %}

  {% endfor %}
  </tbody>
</table>

CodePudding user response:

Thanks I have put the following code which also works. I think it is now more deliberate. The first if creates the opening tag since the next tag should be a pair generated from the divisible these are in the elif the if contains the last tag which should be a closing tag alone since if it where a divisible pair of tags it would create a extra row.

{% extends 'base.html' %}
{% load render_table from django_tables2 %}
{% block content %}

<form method="get">
    {% csrf_token %}
    <table>
        <tbody>
            {% for field in filter.form %}
                {% if forloop.first %}
                    <tr>
                {% endif %}
                    <td>{{ field.label}}</td>
                    <td>{{ field }}</td>
                {% if forloop.last %}
                    </tr>
                {% elif forloop.counter|divisibleby:"4"%}
                    </tr>
                    <tr>
                {% endif %}
            {% endfor %}
        </tbody>
    </table>
    <input type="submit" value="Buscar">
</form>

{% render_table table%}
{% endblock %}
  • Related