【问题标题】:accessing dictionary data from jinja template从 jinja 模板访问字典数据
【发布时间】:2022-01-31 20:44:27
【问题描述】:

我的视图中有一个dictionary,并希望在我的前端呈现它的所有数据。

我的views.py:

def index(request):
    leafVsImdtParents = {
        100: [{'childId': 1, 'childStructure': 'Leaf'}, {'childId': 2, 'childStructure': 'Intermediate'}],
        200: [{'childId': 3, 'childStructure': 'Intermediate'}, {'childId': 4, 'childStructure': 'Leaf'}],
        300: [{'childId': 5, 'childStructure': 'Leaf'}, {'childId': 6, 'childStructure': 'Intermediate'}],
        400: [{'childId': 7, 'childStructure': 'Intermediate'}, {'childId': 8, 'childStructure': 'Leaf'}],
    }
    return render(request,'index.html', {'leafVsImdtParents ': leafVsImdtParents })

在我的templete:

<tbody>
    {% for key, value in leafVsImdtParents %}
    <tr>
        <td>
            {% if key != 0 %}
            <a href="{% url 'diagnosis:ruleTree' ruleId=key %}" class="text-dark fw-bolder text-hover-primary d-block fs-6">
                <span class="badge badge-light-info fs-7"> {{ key }} </span>
            </a>
            {% else %}
            <span class="badge badge-light-danger"> Root Node! </span>
            {% endif %}
        </td>
        <td>
            {% for j in value %}
            <a href="">
                {% if j.childStructure == 'Leaf' %}
                <span class="badge badge-light-success fs-7"> {{ j.childId }} </span>
                {% else %}
                <span class="badge badge-light-info fs-7"> {{ j.childId }} </span>
                {% endif %}
            </a>
            {% endfor %}
            
        </td>
    </tr>
    {% endfor %}
</tbody>

但它显示如下错误:

ValueError at /ruleTreeGroup/
Need 2 values to unpack in for loop; got 1. 

请建议我如何解决这个问题以及如何访问我前端中data 字典中的所有数据

【问题讨论】:

  • 什么是leafVsImdtParents...‽
  • 循环应该是leafVsImdtParents.items,你期望两个值key,value但是在没有items的情况下循环dict只会返回key
  • 它显示如下错误:Could not parse the remainder: '()' from 'leafVsImdtParents.items()'
  • 不会有括号

标签: python html jinja2


【解决方案1】:

这样试试

<tbody>
    {% for key, value in leafVsImdtParents.items %} # this line is changed
    <tr>
        <td>
            {% if key != 0 %}
            <a href="{% url 'diagnosis:ruleTree' ruleId=key %}" class="text-dark fw-bolder text-hover-primary d-block fs-6">
                <span class="badge badge-light-info fs-7"> {{ key }} </span>
            </a>
            {% else %}
            <span class="badge badge-light-danger"> Root Node! </span>
            {% endif %}
        </td>
        <td>
            {% for j in value %}
            <a href="">
                {% if j.childStructure == 'Leaf' %}
                <span class="badge badge-light-success fs-7"> {{ j.childId }} </span>
                {% else %}
                <span class="badge badge-light-info fs-7"> {{ j.childId }} </span>
                {% endif %}
            </a>
            {% endfor %}
            
        </td>
    </tr>
    {% endfor %}
</tbody>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 2019-09-01
    • 2016-02-29
    • 1970-01-01
    • 2021-04-10
    • 2019-08-01
    • 2020-03-16
    • 2022-11-28
    相关资源
    最近更新 更多