Home > Net >  Django: Forloop Counter 0 divisible by 3 not working properly
Django: Forloop Counter 0 divisible by 3 not working properly

Time:07-09

PLATFORM: Django

Problem When using {% forloop.counter0|divisibleby:3 %} it doesn't seem to properly divide out? I can't quite tell what's going on.

Goal Display an Avery HTML template with pre-populated info from the database. 3 column table with variable rows.

Code

<table>
    {% for job in jobsite %}
        {% if forloop.counter0|divisibleby:3 %}<tr>{% endif %}

        <td>{{ job.name }}<br/>{{ job.address }}<br/>{{ job.address_2 }}{% if job.address_2 %}<br/>{% endif %}{{ job.city }}, {{ job.state }} {{ job.zip }}</td>

        {% if forloop.counter < 3 %}
            <td  rowspan="0">&nbsp;</td>
        {% endif %}

        {% if forloop.counter0|divisibleby:3 or forloop.last %}<tr>{% endif %}
    {% endfor %}
</table>

Why is this failing? What's going on here?

Additional Info I can get close if I change the code around to the following. The problem then becomes that counter 2 is blank. If I fill in data, it's duplicated (row 1 column 2, row 1 column 3):

<table>
    <tr>
    {% for job in jobsite %}
        {% if forloop.counter|divisibleby:3 %}</tr><tr>{% endif %}

        <td>{{ job.name }}<br/>{{ job.address }}<br/>{{ job.address_2 }}{% if job.address_2 %}<br/>{% endif %}{{ job.city }}, {{ job.state }} {{ job.zip }}</td>

        {% if forloop.counter < 3 %}
            <td  rowspan="0">&nbsp;</td>
        {% endif %}

        {% if forloop.counter == 2 %}
            <td></td>
{#            <td>{{ job.name }}<br/>{{ job.address }}<br/>{{ job.city }}, {{ job.state }} {{ job.zip }}</td>#}
        {% endif %}
    {% endfor %}
</table>

Close to the desired display Close to right

CodePudding user response:

Problem When using {% if forloop.counter0|divisibleby:3 %} it doesn't seem to properly divide out? I can't quite tell what's going on.

The forloop.counter0 will count with 0, 1, 2, 3, …. This means that the first cycle will already satisfy the condition, since 0 is dividably by 3 (and by any number), so it will already apply the "split logic" for the first cycle. forloop.counter on the other hand will count with 1, 2, 3, 4, … so the first iteration where it will add a line is the third one.

You can work with:

<table>
    <tr>
    {% for job in jobsite %}
        <td>{{ job.name }}<br/>{{ job.address }}<br/>{{ job.address_2 }}{% if job.address_2 %}<br/>{% endif %}{{ job.city }}, {{ job.state }} {{ job.zip }}</td>
        {% if forloop.counter|divisibleby:3 and not forloop.last %}
            </tr><tr>
        {% elif not forloop.last %}
            <td  rowspan="0">&nbsp;</td>
        {% endif %}
    {% endfor %}
    </tr>
</table>

CodePudding user response:

I did not find a solution to this specific challenge, however I redesigned it primarily relying on CSS instead of table layout. Code as follows "solves" the challenge.

{% for job in jobsite %}
    <div >{{ job.name }}<br/>{{ job.address }}<br/>{{ job.address_2 }}{% if job.address_2 %}<br/>{% endif %}{{ job.city }}, {{ job.state }} {{ job.zip }}</div>
{% endfor %}
  • Related