【问题标题】:Unsure how to compute a particular value to be displayed (Python/Django)不确定如何计算要显示的特定值(Python/Django)
【发布时间】:2018-11-20 01:24:26
【问题描述】:

所以基本上在我的商店里,每件商品都有特定的重量,一旦客户添加了他们想要的任何东西并去结账,他们就可以看到他们的每一个订单以及名称信息和重量。我还想把所有物品的总重量加在一起。目前,它仅显示每个特定项目的重量。

例如

  • 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>
    

    【问题讨论】:

    • 您定义了一个total_weight 变量但没有使用它;为什么不在 for 循环中将每个 weight 添加到它,然后将该变量发送到模板?
    • @Danel Roseman 我不确定如何计算它。项目中的项目:total_weight = total_weight + item.supplyID.weight * item.quantity 工作?正如我所做的那样,然后我尝试添加

      {{total_weight}}

      并且它似乎没有工作
    • 但是您已经为每次迭代计算了weight。只需在该循环内执行total_weight += weight。并且不要忘记在您的 render 调用中将其添加到模板上下文中。

    标签: python django


    【解决方案1】:

    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>
    

    【讨论】:

      【解决方案2】:

      首先,您应该了解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>
      

      【讨论】:

        猜你喜欢
        • 2023-01-06
        • 2020-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-11
        • 2019-09-23
        相关资源
        最近更新 更多