dict zip键值Django模板

时间:2018-08-01 10:11:05

标签: django dictionary zip keyvaluepair

配对2个字典值。有效。

pair = dict(zip(hide_dict, fp_dict))
    context = {
        'instance': project,
        'user': user,
        'pair': pair,
    }

我的django html模板部分,出现错误。

  

异常类型:ValueError异常值:需要2个值才能解包   在for循环中;得到了6。

我是否将for循环部分设为false?之前我尝试过配对,但我尝试过单独配对,但效果很好。现在,通过配对,它表明该错误在于上下文渲染中,但我看不到哪里。

 {% for fp_dict.items,hide_dict.items in pair %}
    {% for key, values in hide_dict.items %}
            {%if values == 1%}
            <div style="display:none">
                {% elif  values == 0 %}
                <div>
                    {% endif %}{% endfor %}
             <div class="row">
                <div class="col-sm-12">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>FP Items</th>
                                    </tr>
                                </thead>
                                  <tbody>
                                        <tr>
                                {% for key, values in fp_dict.items %}
                                {% for instance in values %}
                                            <td></td>
                                    <td>{{ instance.FP_Item }}</td>
                                                </a>
                                            </td> -->
                                        </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
{% endfor %}
配对前视图中的

Dict值: 打印(hide_dict) {'hide0':1,1,'hide1':1}

print(fp_dict)

{'fp_list_0': <QuerySet [<FP: olmadan çalışacaktır. - Check - OK - Check - Check - OK - Check - Check - Check>, <FP: depolanabilecek. - Check - OK - Check - Check - OK - Check - Check - Check>, <FP: yönetilebilecektir. - Check - OK - Check - Check - OK - Check - Check
- Check>, '...(remaining elements truncated)...']>}

1 个答案:

答案 0 :(得分:1)

好像您要遍历配对项,因此您需要使用pair.items

{% for fp_dict, hide_dict in pair.items %}
相关问题