简体   繁体   English

如何在Django模板中创建全局变量?

[英]How to create a global variable in django template?

I'm doing a loop inside my template, well, i want verify if my actual object is equal the object that comes before this. 我正在模板中进行循环,那么,我想验证我的实际对象是否等于之前的对象。

This is my tamplate code: 这是我的模板代码:

    {% for i in format_data %}
        <li>
            {% with first=i %}
                {{forloop.counter}}
                {% if forloop.counter == 1 %}

                    <table class="format-table">
                        <tr>
                            <td data-format-id="{{i.format_name.id}}">{{i.format_name}}</td>
                            <td>{{i.field_name}}</td>

                            {% if i.field_format_data %}
                                <td>{{i.field_format_data}}</td>
                            {% else %}
                                <td>Campo não preenchido</td>
                            {% endif %}
                        </tr>
                    </table>
                {% elif second == first %}
                    <table class="format-table">
                        <tr>
                            <td>{{i.field_name}}</td>

                            {% if i.field_format_data %}
                                <td>{{i.field_format_data}}</td>
                            {% else %}
                                <td>Campo não preenchido</td>
                            {% endif %}
                        </tr>
                    </table>
                {% endif %}
            {% endwith %}
            {% with second=i %}
            {% endwith %}
        </li>
    {% endfor %}

What I want is set a variable in the final of the loop that receive this actual object and in the next time that loop works, verify if the actual object is equal the last 我想要的是在接收该实际对象的循环的最后设置一个变量,并在下次该循环工作时,验证实际对象是否等于最后一个对象

You can't create a global variable in a template, and really you should avoid trying to create variables there at all. 您不能在模板中创建全局变量,实际上应该避免尝试在模板中创建变量。

If you want to check a value between iterations, you can use the cycle tag. 如果要在两次迭代之间检查一个值,则可以使用cycle标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM