gpt4 book ai didi

python - Django 类型错误未提供异常

转载 作者:行者123 更新时间:2023-11-28 17:43:55 25 4
gpt4 key购买 nike

你好这是我比较两个手机技术规范的代码

def getFormValues(request):
if ('mobile_a' in request.GET and request.GET['mobile_a']) and ('mobile_b' in request.GET and request.GET['mobile_b']):
mobile_a = request.GET['mobile_a']
mobile_b =request.GET['mobile_b']
# calling the mark calculation function
return calculateMark(mobile_a, mobile_b)

else:
message ='You submitted an empty form.'
return HttpResponse(message)

def calculateMark(mobile_a, mobile_b):
#variables
mobile_a = mobile_a
mobile_b = mobile_b
tech_mark_a= 0
tech_mark_b = 0

results_a = []
results_b = []

record_a = TechSpecificationAdd.objects.filter(mobile_name=mobile_a).values()
record_b = TechSpecificationAdd.objects.filter(mobile_name=mobile_a).values()

results_a += record_a
results_b += record_b

#dimension
if int(record_a["dimension"]) > int(record_b["dimension"]):
tech_mark_a = 1
else:
tech_mark_b = 1


#body-material

#weight
if record_a["weight"] > record_b["weight"]:
tech_mark_b += 1
else:
tech_mark_a += 1

#camera
if record_a["camera"] > record_b["camera"]:
tech_mark_a += 1
else:
tech_mark_b += 1

#flash
if (record_a["flash"]):
tech_mark_a += 1
if (record_b["flash"]):
tech_mark_b += 1

#video
if record_a["video"] > record_b["video"]:
tech_mark_a += 1
else:
tech_mark_b += 1

#fps
if record_a["fps"] > record_b["fps"]:
tech_mark_a += 1
else:
tech_mark_b += 1

#front-camera
if record_a["front_camera"] > record_b["front_camera"]:
tech_mark_a += 1
else:
tech_mark_b += 1


return render_to_response('degrees_result.html', {'data_a': results_a, 'data_b': results_b})

在这种情况下,django 调试显示错误“TypeError at/calculate_mark/没有异常(exception)。如果你要求追溯信息,我可以提供。

那么问题是什么?我想不通。

最佳答案

QuerySet.values返回 ValuesQuerySet 对象(类似于字典列表)

record_a = TechSpecificationAdd.objects.filter(mobile_name=mobile_a).values()

但是代码将它当作字典来使用:

int(record_a["dimension"])

转换如下用法:

int(record_a[0]["dimension"])

或者将record_a转换为字典:

record_a = record_a[0]

关于python - Django 类型错误未提供异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com