Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

accessing dictionary data from jinja template

I have a dictionary in my views and want to render all of it’s data in my frontend.

my 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 })

In my templete:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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>

But it shows error like:

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

Please suggest how can I solve fix this and how to access all the data from data dictionary in my frontend

>Solution :

try like this

<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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading