django学习笔记---模板语言forloop循环和filter条件过滤

在html中写上

  <div><h3>对象 </h3></div>
    <table border="1">
        <thead>
            <tr>
                <td>序号</td>
                <td>主机名</td>
                <td>ip</td>
                <td>端口</td>
                <td>业务线名称</td>
            </tr>
        </thead>
        <tbody>
        {% for i in w1 %}
            {% for row in w1 %}
                <tr hid="{{ row.nid }}" >    <!--不显示nid,可以存放在tr中,便于以后取,目前不知道怎么取 -->
                    <td>{{ forloop.parentloop }}</td>
                    <td>{{ row.hostname }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.b.caption }}</td>
                </tr>
            {% endfor %}
        {% endfor %}
        </tbody>
    </table>

如果有嵌套循环,那么parentloop就会输出他的父循环。在这张图中,forloop有forloop.counter0,revcounter0,counter,revcounter,first,last ,在下图每个意思一目了然。

比如,把代码改成forloop.parentloop.counter 

      {% for i in w1 %}
            {% for row in w1 %}
                <tr hid="{{ row.nid }}" >   
                    <td>{{ forloop.parentloop.counter }}</td>
                    <td>{{ row.hostname }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.b.caption }}</td>
                </tr>
            {% endfor %}
        {% endfor %}

那么输出的序号就是1,1,1,1,2,2,2,2....即父循环的索引


filter


exclude(id=2)  排除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值