嗨,我有一个 ajax 调用,它与一个小视图相关联:
def get_spans(snow_load, wind_speed, module_length):
spans = Spans.objects.values_list('span').filter(
snow=snow_load,
wind=wind_speed,
module_length__gte=module_length,
).order_by('span')
try:
max_span = max(spans)
except ValueError:
max_span = 0
return max_span
def GetSpans(request):
# get variables from ajax request
snow_load = request.GET.get('snow_load', None)
wind_speed = request.GET.get('wind_speed', None)
module_length = request.GET.get('module_length', None)
# call get_spans()
max_span = get_spans(snow_load, wind_speed, module_length)
# json encode it
max_span = simplejson.dumps(max_span, cls=DjangoJSONEncoder)
return HttpResponse(
max_span,
mimetype='application/json'
)
问题是这些 get 变量中的任何一个都可能是''
小数。我的问题是处理这些问题的最佳方法是什么?如果变量是''
我得到invalid literal for int() with base 10:
错误。None
我应该用例如替换所有空字符串吗?我应该将所有值转换为float
吗?
非常感谢任何帮助