Py学习  »  Python

不确定如何计算要显示的特定值(python/django)

Nayo xx • 4 年前 • 364 次点击  

所以基本上在我的商店里,每件商品都有一个特定的重量,一旦顾客想加什么就加什么,然后去结账,他们就可以看到他们的每一个订单以及名字信息和重量。我还想把所有物品的总重量加在一起。目前,它只显示每个特定项目的权重。

例如

  • A项为2公斤,B项为3公斤
  • 如果客户添加2项A和3项B
  • 显示项目:A数量:2重量:4kg
  • 项目:B数量:3重量:9kg。
  • 我还要加上总重量:13公斤
  • 这是我的观点.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))
                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>
    
    Python社区是高质量的Python/Django开发社区
    本文地址:http://www.python88.com/topic/47140
     
    364 次点击  
    文章 [ 2 ]  |  最新文章 4 年前