在Django视图和模板中执行一些算术运算后,我得到了错误的值 [英] I have getting wrong values after perrforming some arithmetic in Django views and templates

查看:73
本文介绍了在Django视图和模板中执行一些算术运算后,我得到了错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从模板中获取一些数据,执行一些算术运算并将其显示给最终用户,我注意到手动计算结果后得到了错误的答案,因此我尝试打印出已经从控制台上的模板中抢了下来,我得到了正确的答案,但最终的计算中有一些错误.

I am trying to grab some data from the template, perform some arithmetic and display it to the end-users, I noticed that I am getting the wrong answers after manually calculating the result, so I tried to print the values which I have grabbed from my templates on the console and I get the right answer but the final calculation is having some errors in it.

我不知道如何获取"NET_COST","PROFIT"和"ROI"对于该消息,这就是为什么我使用了静态的"$ 100".我几乎不知道"f字符串"的含义.和格式"但是我不知道如何在这里应用它.

I don't know how to grab the "NET_COST", "PROFIT" and "ROI" to the message, that's why I have used a static "$100". I know little how "f strings" and "formats" but I don't know how to apply it here.

这些是我以表格形式输入的值:

These are the values I am entering in the form:

views.py

def Profitcalculator(request):

    if request.method == 'POST':
        SP = request.POST.get("sp")
        fees_on_sp = request.POST.get("fees-on-sp")
        transport = request.POST.get("transport")
        extra_cost = request.POST.get("extra-cost")
        product_cost = request.POST.get("product-cost")
        ADS = request.POST.get("ads")
        GIVEAWAY = request.POST.get("giveaway")

        NET_COST = (fees_on_sp + transport + extra_cost + product_cost + ADS + GIVEAWAY)
        PROFIT = (int(SP) - int(NET_COST))
        ROI = (int(PROFIT)/int(NET_COST)) * 100
        print(SP, ADS, GIVEAWAY)
        print(NET_COST, PROFIT, ROI)

        messages.info(request, "Your NET_COST is $100")

    return render(request, 'core/profit-calculator.html')

控制台

600 2 7
510425027 -510424427 -99.99988245090498

profit-calculator.html

profit-calculator.html

<form method="post">
{% csrf_token %}
<div class="row form-group">
    <div class="col-md-4">
        <input class="m-t-10 au-input au-input--full" type="number" name="sp" placeholder="Selling Price" required>
    </div>
    <div class="col-md-4">
        <input class="m-t-10 au-input au-input--full" type="number" name="fees-on-sp" placeholder="Estimated Fees on selling platform(per unit)" required>
    </div>
    <div class="col-md-4">
        <input class="m-t-10 au-input au-input--full" type="number" name="transport" placeholder="Total transport Cost(per unit)" required>
    </div>
</div>

<div class="row form-group">
    <div class="col-md-3">
        <input class="m-t-10 au-input au-input--full" type="number" name="extra-cost" placeholder="Any extra cost(per unit)" required>
    </div>
    <div class="col-md-3">
        <input class="m-t-10 au-input au-input--full" type="number" name="product-cost" placeholder="Product Cost" required>
    </div>
    <div class="col-md-3">
        <input class="m-t-10 au-input au-input--full" type="number" name="ads" placeholder="Ads(per unit)" required>
    </div>
    <div class="col-md-3">
        <input class="m-t-10 au-input au-input--full" type="number" name="giveaway" placeholder="Total Cost per promotional giveaway" required>
    </div>
</div>

<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Calculate Net Cost, Profit and ROI Percentage</button>
</form>

推荐答案

您需要将所有 POST 值转换为 int .然后您可以进行计算.

You need to convert all your POST values to int. And then you can do your calculations.

def Profitcalculator(request):

    if request.method == 'POST':
        query = request.POST
        SP = int(query.get("sp") or 0)
        fees_on_sp = int(query.get("fees-on-sp") or 0)
        transport = int(query.get("transport") or 0)
        extra_cost = int(query.get("extra-cost") or 0)
        product_cost = int(query.get("product-cost") or 0)
        ADS = int(query.get("ads") or 0)
        GIVEAWAY = int(query.get("giveaway") or 0)

        NET_COST = fees_on_sp + transport + extra_cost + product_cost + ADS + GIVEAWAY
        PROFIT = SP - NET_COST
        ROI = (PROFIT/NET_COST) * 100
        print(SP, ADS, GIVEAWAY)
        print(NET_COST, PROFIT, ROI)

        messages.info(request, f"Your Net Cost is ${NET_COST}")

    return render(request, 'core/profit-calculator.html')

这篇关于在Django视图和模板中执行一些算术运算后,我得到了错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆