我知道以前曾问过这个问题,但就我的情况而言,我似乎无法弄清楚为什么会抛出这个问题
当我尝试运行计算时,控制台出现以下错误:
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')
int() 函数抛出异常,因为您试图转换非数字的内容。
我建议您可以考虑使用调试打印语句来找出计划和 fiid 最初是什么以及它们如何变化。
您可以做的另一件事是使用 try/catch 包装对 int() 的调用
val='a'
try:
int_val = int(val)
except ValueError:
print("Failure w/ value " + val)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)