Py学习  »  Python

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

Nayo xx • 4 年前 • 358 次点击  

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

例如

  • 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
     
    358 次点击  
    文章 [ 2 ]  |  最新文章 4 年前
    Rarblack
    Reply   •   1 楼
    Rarblack    5 年前

    首先,你应该明白 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>
    
    Michael King
    Reply   •   2 楼
    Michael King    5 年前

    Documentation

    传递给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>