Issue
I have this code :
{% for group in grouped_fields %}
<tr>
{% if forloop.counter0|divisibleby:2 %}
{% with 'bgcolor2' as row_color %}
{% else %}
{% with 'bgcolor1' as row_color %}
{% endif %}
{% for field in group %}
{% if forloop.first %}
<td class="w-12 bigcell {{ row_color }}">
<input maxlength="1" size="1" class="text-center bg-white">
</td>
{% else %}
{% if forloop.counter0|add:forloop.parentloop.counter0|divisibleby:2 %}
{% with 'bgcolor2' as cell_color %}
{% else %}
{% with 'bgcolor1' as cell_color %}
{% endif %}
<td class="w-11 text-center {{ cell_color }}">
<input class="text-center" size="4" placeholder="yy">
<br>
<span>xx</span>
</td>
{% if forloop.last and forloop.parentloop.last %}
<td class="w-12 bigcell {{ row_color }}">اول</td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
that I copied from ChatGPT but it seems it does not include correct form of with. There should be an {%endwith%} somewhere in the code so there wont be an error. I cant figureout how to use the template tag correctly.
can someone aid me with this
Solution
Please don't use ChatGPT as a device that writes your code. ChatGPT might automate some tasks, yes. But you need to at least understand what it is writing. Chatbots like ChatGPT might seem to have some intelligence, but these are just statistical word sequence generators.
As for the problem itself, it simply misues with as a way to assign a value to a variable, but that is not possible: a {% with … %}
statement is used to assign a value to a variable for a limited scope.
Here we can use {% cycle … %}
[Django-doc] here:
{% for group in grouped_fields %}
<tr>
{% cycle 'bgcolor2' 'bgcolor1' as row_color silent %}
{% for field in group %}
{% cycle 'bgcolor2' 'bgcolor1' as cell_color silent %}
{% if forloop.first %}
<td class="w-12 bigcell {{ row_color }}">
<input maxlength="1" size="1" class="text-center bg-white">
</td>
{% else %}
<td class="w-11 text-center {{ cell_color }}">
<input class="text-center" size="4" placeholder="yy">
<br>
<span>xx</span>
</td>
{% if forloop.last and forloop.parentloop.last %}
<td class="w-12 bigcell {{ row_color }}">اول</td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.