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.

  • What do you mean by **not working** ? Explain a bit whats happening. – Biplove Lamichhane Aug 03 '21 at 03:49
  • Well, actually if i use`{{value}}` then, i can get the all the value. But if i want to see get only **Name** or only **CustomsCharges** using `{{value.Name}}` then is not giving value. I am willing to access specific value. – Azharul Islam Somon Aug 03 '21 at 04:03

1 Answers1

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 %}
binpy
  • 3,994
  • 3
  • 17
  • 54