Unsure how to compute a particular value to be displayed (Python/Django)












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>









    share|improve this question


















    • 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 <p>{{total_weight}}</p> 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


















    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>









    share|improve this question


















    • 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 <p>{{total_weight}}</p> 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
















    1












    1








    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>









    share|improve this question














    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>






    python django






    share|improve this question













    share|improve this question











    share|improve this question




    share|improve this question










    asked Nov 19 '18 at 16:42









    Nayo xx

    62




    62








    • 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 <p>{{total_weight}}</p> 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




      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 <p>{{total_weight}}</p> 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




    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




    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 <p>{{total_weight}}</p> and it didn't seem to work
    – Nayo xx
    Nov 19 '18 at 16:49






    @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 <p>{{total_weight}}</p> 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






    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 Answers
    2






    active

    oldest

    votes


















    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>





    share|improve this answer





























      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>





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379136%2funsure-how-to-compute-a-particular-value-to-be-displayed-python-django%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        2 Answers
        2






        active

        oldest

        votes








        2 Answers
        2






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        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>





        share|improve this answer


























          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>





          share|improve this answer
























            0












            0








            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>





            share|improve this answer












            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>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 17:04









            Michael King

            262




            262

























                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>





                share|improve this answer




























                  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>





                  share|improve this answer


























                    0












                    0








                    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>





                    share|improve this answer














                    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>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 19 '18 at 17:24

























                    answered Nov 19 '18 at 17:15









                    Rarblack

                    2,7293925




                    2,7293925






























                        draft saved

                        draft discarded




















































                        Thanks for contributing an answer to Stack Overflow!


                        • Please be sure to answer the question. Provide details and share your research!

                        But avoid



                        • Asking for help, clarification, or responding to other answers.

                        • Making statements based on opinion; back them up with references or personal experience.


                        To learn more, see our tips on writing great answers.





                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                        Please pay close attention to the following guidance:


                        • Please be sure to answer the question. Provide details and share your research!

                        But avoid



                        • Asking for help, clarification, or responding to other answers.

                        • Making statements based on opinion; back them up with references or personal experience.


                        To learn more, see our tips on writing great answers.




                        draft saved


                        draft discarded














                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379136%2funsure-how-to-compute-a-particular-value-to-be-displayed-python-django%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown





















































                        Required, but never shown














                        Required, but never shown












                        Required, but never shown







                        Required, but never shown

































                        Required, but never shown














                        Required, but never shown












                        Required, but never shown







                        Required, but never shown







                        Popular posts from this blog

                        'app-layout' is not a known element: how to share Component with different Modules

                        android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                        WPF add header to Image with URL pettitions [duplicate]