I am trying to initialize the variable and increment it in the for loop but I'm getting the following error:
Invalid block tag: 'set', expected 'endspaceless'
This is my code:
<tr{{ row.attr_string|safe }}>
{% spaceless %}
{% set counter = 0 %}
{% for cell in row %}
{% set counter= counter+1 %}
{% if counter < 4 %}
{% include "horizon/common/_data_grid_cell.html" %}
{% else %}
{% include "horizon/common/_data_table_cell.html" %}
{% endif %}
{% endfor %}
{% endspaceless %}
</tr>
You don't need to. Django already comes with a forloop.counter, as well as a forloop.counter0
. You can use it directly:
<tr{{ row.attr_string|safe }}>
{% spaceless %}
{% for cell in row %}
{% if forloop.counter < 4 %}
{% include "horizon/common/_data_grid_cell.html" %}
{% else %}
{% include "horizon/common/_data_table_cell.html" %}
{% endif %}
{% endfor %}
{% endspaceless %}
</tr>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With