django模板中for语句同时遍历两个列表

首先,如果在模板中直接遍历两个列表是会报错的,因为不支持。
那么,我们怎么解决?
有办法,在视图中先把两个列表用zip()函数打包即可,这样,就可以在模板中对两个列表同时for输出了

视图:

def personal(request,account):
    account=account
    user = User.objects.get(account=account)
    icon_path=user.img
    #此处是将两个列表打包
    information=['昵称','账号','性别','注册时间']
    information2=[user.name,user.account,user.gender,user.create_time]
    user2=zip(information,information2)
    return render(request,'personal.html',{'user':user2,'icon':icon_path})

模板:

{% for x,y in user %}
            {% if y == True %}
            <p><label for="{{ x }}">{{ x }}:--------->></label><span>  </span></p>
            {% elif y == False %}
            <p><label for="{{ x }}">{{ x }}:--------->></label><span>  </span></p>
            {% else %}
            <p><label for="{{ x }}">{{ x }}:--------->></label><span>  {{ y }}</span></p>
            {% endif %}
    {% endfor %}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值