You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中{% with %}模板标签正确使用的问题求助

Django中{% with %}模板标签正确使用的问题求助

嗨,我来帮你搞定这个Django模板标签的问题!你遇到的错误确实是因为{% with %}标签没有正确闭合——Django要求每个{% with %}必须对应一个{% endwith %},用来标记这个变量的作用范围结束,不然模板解析器肯定会报错的。

先看你原来的代码问题:不管是给row_color还是cell_color赋值的地方,都只写了开启的{% with %},但完全没加闭合的{% endwith %},这就是错误根源。

我给你两种修正方案,你可以选自己习惯的:

方案一:补全原来if-else结构的{% endwith %}

这种是严格按照你原来的逻辑,把每个开启的with块都正确闭合:

{% for group in grouped_fields %}
<tr>
    {% if forloop.counter0|divisibleby:2 %}
        {% with 'bgcolor2' as row_color %}
            {% 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 %}
                            <td class="w-11 text-center {{ cell_color }}">
                                <input class="text-center" size="4" placeholder="yy">
                                <br>
                                <span>xx</span>
                            </td>
                        {% endwith %} {# 闭合cell_color的with #}
                    {% else %}
                        {% with 'bgcolor1' as cell_color %}
                            <td class="w-11 text-center {{ cell_color }}">
                                <input class="text-center" size="4" placeholder="yy">
                                <br>
                                <span>xx</span>
                            </td>
                        {% endwith %} {# 闭合cell_color的with #}
                    {% endif %}
                    {% if forloop.last and forloop.parentloop.last %}
                        <td class="w-12 bigcell {{ row_color }}">اول</td>
                    {% endif %}
                {% endif %}
            {% endfor %}
        {% endwith %} {# 闭合row_color的with #}
    {% else %}
        {% with 'bgcolor1' as row_color %}
            {% 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 %}
                            <td class="w-11 text-center {{ cell_color }}">
                                <input class="text-center" size="4" placeholder="yy">
                                <br>
                                <span>xx</span>
                            </td>
                        {% endwith %} {# 闭合cell_color的with #}
                    {% else %}
                        {% with 'bgcolor1' as cell_color %}
                            <td class="w-11 text-center {{ cell_color }}">
                                <input class="text-center" size="4" placeholder="yy">
                                <br>
                                <span>xx</span>
                            </td>
                        {% endwith %} {# 闭合cell_color的with #}
                    {% endif %}
                    {% if forloop.last and forloop.parentloop.last %}
                        <td class="w-12 bigcell {{ row_color }}">اول</td>
                    {% endif %}
                {% endif %}
            {% endfor %}
        {% endwith %} {# 闭合row_color的with #}
    {% endif %}
</tr>
{% endfor %}

方案二:用yesno过滤器简化代码(更优雅)

其实可以不用写那么多if-else,Django的yesno过滤器可以根据布尔值直接返回对应的字符串,这样代码更简洁,而且with标签的使用也更清晰:

{% for group in grouped_fields %}
<tr>
    {# 根据外层循环奇偶性直接赋值row_color #}
    {% with row_color=forloop.counter0|divisibleby:2|yesno:"bgcolor2,bgcolor1" %}
        {% 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 %}
                {# 根据内外层循环的计数器之和奇偶性赋值cell_color #}
                {% with cell_color=forloop.counter0|add:forloop.parentloop.counter0|divisibleby:2|yesno:"bgcolor2,bgcolor1" %}
                    <td class="w-11 text-center {{ cell_color }}">
                        <input class="text-center" size="4" placeholder="yy">
                        <br>
                        <span>xx</span>
                    </td>
                {% endwith %} {# 闭合cell_color的with #}
                
                {% if forloop.last and forloop.parentloop.last %}
                    <td class="w-12 bigcell {{ row_color }}">اول</td>
                {% endif %}
            {% endif %}
        {% endfor %}
    {% endwith %} {# 闭合row_color的with #}
</tr>
{% endfor %}

这样两种写法都能解决你的问题,而且完全符合Django模板标签的语法要求,不会再报with标签未闭合的错误啦。

备注:内容来源于stack exchange,提问作者Essiprog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 11:14:33