3

我知道以前有人问过这个问题,但就我的情况而言,我似乎无法弄清楚为什么会抛出这个问题

当我尝试运行我的计算时,我的控制台给出了这个错误:

ValueError: invalid literal for int() with base 10: ''

它说它来自

File "/var/sites/live_mbpathways/moneybroker/apps/investments/ajax.py", line 30, in calc_cdic
    investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by('maturity_date').exclude(id=investment_id)

有谁知道为什么会发生这种情况?

这是我的 ajax.py 代码所在的位置:

@login_required
@moneybroker_auth
@csrf_exempt
def calc_cdic(request, plan, investment_id=None, *args, **kwargs):
    from investments.models import Investment
    from financial_institutions.models import FinancialInstitution
    from profiles.models import Profile
    from plans.models import Plan
    from datetime import datetime
    now = datetime.now()
    json = {}
    data = request.POST

    if request.is_ajax():
        total = 0
        fiid = data.get('financial_institution')
        amt = data.get('amount') or 0
        pay_amt = data.get('pay_amount') or 0
        mat_amt = data.get('maturity_amount') or 0
        investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by('maturity_date').exclude(id=investment_id)
        for i in investments:
            total += i.maturity_amount
        print total
        json['total'] = float(str(total))
        json['amt'] = float(str(amt))
        json['pay_amt'] = float(str(pay_amt))
        json['mat_amount'] = float(str(mat_amt))
        json['fiid'] = fiid
        print json

    return HttpResponse(simplejson.dumps(json), mimetype='application/json')
4

4 回答 4

5

int() 函数抛出异常,因为您试图转换不是数字的东西。

我建议您可以考虑使用调试打印语句来找出最初的plan 和fiid 是什么以及它们是如何变化的。

您可以做的另一件事是使用 try/catch 包装对 int() 的调用

val='a'
try:
    int_val = int(val)
except ValueError:
    print("Failure w/ value " + val)
于 2012-05-16T23:17:21.157 回答
3

在该调用链中的某处,您传递了一个空字符串,然后代码试图将其转换为整数。记录您当时的输入以跟踪它。

于 2012-05-16T14:38:09.467 回答
0

建议您查看 and 的值planfiid因为planorfiid没有设置为您认为的值。

于 2012-05-16T14:50:48.713 回答
0

对我来说,我必须删除迁移文件夹并按照此链接中最佳答案中的步骤操作,然后重新运行迁移,然后错误消失并且它起作用了。

于 2016-06-02T14:44:50.447 回答