因此,基本上在我的商店中,每件商品都有特定的重量,一旦客户添加了他们想要的东西并去结帐,他们就可以看到他们的每个订单以及名称信息和重量。我还想将所有物品的总重量相加。当前,它仅显示每个特定项目的重量。
例如
def checkout(request):
try:
current_order = Order.objects.filter(owner=1).get(status="pre-place")
except Order.DoesNotExist:
return HttpResponse("Your current order is empty<br><a href=\"browse\">Go back</a>")
else:
total_weight = 0
items = OrderDetail.objects.filter(orderID=current_order)
template_name = 'store/checkout.html'
order_details = []
for item in items:
weight = item.supplyID.weight * item.quantity
order_details.append((item, weight))
return render(request, template_name, {'order_details': order_details, 'current_order': current_order})
<h1>Your current order</h1>
<a href="{% url 'Store:browse' %}">return to selecting
supplies</a><br><br>
<table>
<tr><th>name</th><th>item weight(kg)</th><th>qty</th><th>total
weight(kg)</th></tr>
{% for order_detail, weight in order_details %}
<tr>
<td>{{ order_detail.supplyID.name }}</td>
<td>{{ order_detail.supplyID.weight }}</td>
<td>{{ order_detail.quantity }}</td>
<td>{{ weight }}</td>
</tr>
{% endfor %}
</table>
答案 0 :(得分:0)
传递给render的上下文变量只必须是一个字典,因此您可以在views.py中计算总权重,将此值放在字典中,然后在模板中获取总权重键的值
例如:
def checkout(request):
try:
current_order = Order.objects.filter(owner=1).get(status="pre-place")
except Order.DoesNotExist:
return HttpResponse("Your current order is empty<br><a href=\"browse\">Go back</a>")
else:
total_weight = 0
items = OrderDetail.objects.filter(orderID=current_order)
template_name = 'store/checkout.html'
order_details = []
for item in items:
weight = item.supplyID.weight * item.quantity
order_details.append((item, weight))
total_weight +=weight
return render(request, template_name, {'order_details': order_details, 'current_order': current_order, 'Total Weight' : total_weight})
然后在模板中使用该变量:
<h1>Your current order</h1>
<a href="{% url 'Store:browse' %}">return to selecting supplies</a><br><br>
<table>
<tr>
<th>name</th><th>item weight(kg)</th><th>qty</th><th>total weight(kg)</th>
</tr>
{% for order_detail, weight in order_details %}
<tr>
<td>{{ order_detail.supplyID.name }}</td>
<td>{{ order_detail.supplyID.weight }}</td>
<td>{{ order_detail.quantity }}</td>
<td>{{ weight }}</td>
</tr>
{% endfor %}
</table>
<p>The total weight of your order is:</p>
<p>{{Total Weight}}</p>
答案 1 :(得分:0)
首先,您应该了解get()
和filter()
之间的区别。看看this。
之后,我们可以进行一些更改:
def checkout(request):
try:
current_order = Order.objects.filter(owner__exact=1, status__icontains ="pre-place") # exact returns exact match, icontains(could have been iexact too if you want exact match) return not case sensitive match.
except Order.DoesNotExist:
return HttpResponse("Your current order is empty<br><a href=\"browse\">Go back</a>")
else:
items = OrderDetail.objects.filter(orderID__exact=current_order) #since it is id no need for iexact which is case insensitive.
order_details = {} # it is always suggestible to use dictionary instead of turple for easiness.
for item in items:
weight = item.supplyID.weight * item.quantity
order_details[item] = weight
total_weight = sum(order_detail.values()) #sum of total weight
context = { #clear to read and maintain
'order_details': order_details,
'current_order': current_order,
'total_weight': total_weight
}
return render(request,
'store/checkout.html', # i don't find storing url usefull
context=context)
这是您的模板:
<h1>Your current order</h1>
<a href="{% url 'Store:browse' %}">return to selecting
supplies</a><br><br>
<table>
<tr><th>name</th><th>item weight(kg)</th><th>qty</th><th>total
weight(kg)</th></tr>
{% for item, weight in order_details.items() %}
<tr>
<td>{{ item.supplyID.name }}</td>
<td>{{ item.supplyID.weight }}</td>
<td>{{ item.quantity }}</td>
<td>{{ weight }}</td>
</tr>
{% endfor %}
</table>