Reputation: 31
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.
Upvotes: 1
Views: 1650
Reputation: 4194
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 %}
Upvotes: 2