1

I store JSON data to TextField : Data in TextField:

[{"id":1,"Name":"AC","Quantity":"12","Weight":"200","WeightTypes":"KG",
 "TotalWeight":"2400","CustomsCharge":"300.0","Subtotal":"3600"},

{"id":2,"Name":"Soap","Quantity":"12","Weight":"500",
 "WeightTypes":"GRAM","TotalWeight":"6","Customs Charge":"0.0","Subtotal":"12"}]

I receive data in my view.py: using products = json.loads(data)

After that, i am trying to show each item's in Django templates

{% for p in products %}
    {% for key, value in p.items %}
            <tr>
                <td>{{value.id}}</td>
                <td>{{value.Name}}</td>
                <td>{{value.Quantity}}</td>
                <td>{{value.CustomsCharge}}</td>
                <td>{{value.Subtotal}}</td>
                
            </tr>
        {% endfor %}
            
    {% endfor %} 

But it's not working! How can i get each value from this Jason field?

Thank you in advance.

2
  • What do you mean by not working ? Explain a bit whats happening. Commented Aug 3, 2021 at 3:49
  • Well, actually if i use<td>{{value}}</td> then, i can get the all the value. But if i want to see get only Name or only CustomsCharges using <td>{{value.Name}}</td> then is not giving value. I am willing to access specific value. Commented Aug 3, 2021 at 4:03

1 Answer 1

2

I think you just wrong on parsing the data into template. Basicly you no need to use p.items because it will unpack your original dict items values, meanwhile yo just need to directly call on product loops.

{% for p in products %}
    <tr>
        <td>{{ p.id }}</td>
        <td>{{ p.Name }}</td>
        <td>{{ p.Quantity }} (pcs)</td>
        <td>{{ p.Weight }}</td>
        <td>{{ p.WeightTypes }}</td>
        <td>{{ p.TotalWeight }}</td>
        <td>{{ p.CustomsCharge }}</td>
        <td>{{ p.Subtotal }}</td>
    </tr>
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.