如何在我的模板中使用過濾器將值放入行匹配的行中。在模板中過濾以在django中專門安排數據
因此,例如內部OD密鑰行1中的所有值都應該具有該密鑰的值 ,則在第二行中應該具有行2值的Outter OD密鑰的所有值。
任何幫助,將不勝感激
這裏是我的view.py
@login_required
def shipping(request, id):
sheet_data = Sheet.objects.get(pk=id)
work_order = sheet_data.work_order
customer_data = Customer.objects.get(id=sheet_data.customer_id)
customer_name = customer_data.customer_name
title_head = 'Shipping-%s' % sheet_data.work_order
complete_data = Sheet.objects.raw("""select s.id, d.id d_id, s.work_order, d.target, i.reading, d.description, i.serial_number from app_sheet s left join app_dimension d on s.id = d.sheet_id
left join app_inspection_vals i on d.id = i.dimension_id""")
for c_d in complete_data:
dim_description = Dimension.objects.filter(sheet_id=c_d.id).values_list('description', flat=True).distinct()
dim_id = Dimension.objects.filter(sheet_id=c_d.id)[:1]
for d_i in dim_id:
dim_data = Inspection_vals.objects.filter(dimension_id=d_i.id)
reading_data = Inspection_vals.objects.filter(dimension_id=d_i.id)
key_list = []
vals_list = []
for xr in complete_data:
key_list.append(xr.description)
vals_list.append(xr.reading)
#print reading_desc
sample_size = dim_data
res = {}
for i in range(len(key_list)):
if key_list[i] in res:
res[key_list[i]].append(vals_list[i])
else:
res[key_list[i]]=[vals_list[i]]
reading_desc = res
return render(request, 'app/shipping.html',
{
'work_order': work_order,
'sample_size': sample_size,
'customer_name': customer_name,
'title': title_head,
'complete_data': complete_data,
'dim_description': dim_description,
'reading_desc': reading_desc,
})
這裏是reading_desc的是正確地使用每一個正確的密鑰和值輸出。
{u'Inner OD': [2, None, None, 1, 1, 1, 6, 1, 2, 4, 5, 5, 7, 1, 1, 2, 3, 4, 5, 1, None, 3, 4, 6], u'Outter OD': [3, 4, 5, 1, 3, 2, 2, 2, 2, 1, 1, 1, 1, 3, 4, 3, 5, 6, 5, 2, 3]}
這裏是我的模板shipping.html
<div class="container">
<div class="row">
<div>
<table >
<thead>
<tr>
<th>Serial Number</th>
{% for ss in sample_size %}
<th>{{ ss.serial_number }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for desc in dim_description.all %}
<tr>
<th> {{ desc }}</th>
{% for r_c in reading_desc %}
<td class="{% cycle r_c, r_c %}"> {{ r_c }} </td>
{% endfor %}
{% endfor %}
</tr>
</tbody>
</table>
</div>
</div>
</div>
這是我想它看起來像
所以你問的是如何迭代字典並以表格格式顯示鍵和值? –
基本上是@MichaelPlatt – Snowman08