gpt4 book ai didi

django - 如何在 Django 模板中初始化变量并增加它?

转载 作者:行者123 更新时间:2023-12-04 23:19:54 26 4
gpt4 key购买 nike

我正在尝试初始化变量并在 for 循环中递增它,但出现以下错误:

无效的块标记:“set”,应为“endspaceless”

这是我的代码:

<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>

最佳答案

你不需要。 Django 已经带有 forloop.counter ,以及 forloop.counter0 .您可以直接使用它:

<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>

关于django - 如何在 Django 模板中初始化变量并增加它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com