1

So basically in my store, every item has a specific weight and once the customer adds whatever they want and go to checkout, they can see every single order of theirs along with the name information and weight. I want to also add the total weight of all the items together. Currently, it displays only the weight of each particular item.

For example

  • Item A is 2 Kg and item B is 3 kg
  • If customer adds 2 item A and 3 Item B
  • It displays Item: A quantity: 2 Weight : 4kg
  • Item: B quantity: 3 Weight: 9kg.
  • I want to also add Total weight : 13 kg
  • This is my 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))
                return render(request, template_name, {'order_details': order_details, 'current_order': current_order})
    

    This is my template

    <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>
    
    Nayo xx
    • 11
    • 2
    • 2
      You defined a `total_weight` variable but didn't use it; why don't you add each `weight` to it within the for loop, then send that variable to the template? – Daniel Roseman Nov 19 '18 at 16:46
    • @Danel Roseman I'm not sure how to go about computing it. would for item in items: total_weight = total_weight + item.supplyID.weight * item.quantity work? As I did the same and then I tried adding

      {{total_weight}}

      and it didn't seem to work
      – Nayo xx Nov 19 '18 at 16:49
    • But you already calculated `weight` for each iteration. Just do `total_weight += weight` inside that loop. And don't forget to add it to the template context in your `render` call. – Daniel Roseman Nov 19 '18 at 16:51

    2 Answers2

    0

    Documentation

    The context variable passed to render just has to be a dictionary , so you can do your computation of the Total Weight in views.py, place this value in the dictionary and then grab the value of the Total Weight key in your template.

    For example:

    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})
    

    Then just use that variable in your template:

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

    First, you should understand the difference between get() and filter(). Take a look at this.

    After that we can make some changes:

    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)
    

    This is your template:

    <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>
    
    Rarblack
    • 4,559
    • 4
    • 22
    • 33