在Django项目中,我有一个javascript,它将图像的某些坐标发送到Django表单。然后用户提交表单,在应用某些逻辑后,坐标将保存到数据库中。大部分时间它运作良好但有时javascript(我有限制访问)发送这种数字:55.46353234234234e-14并在表单数据中我得到这种变量:u'middle_img_center_y':[u'NaN ']而不是我通常得到的数字。然后当然我得到了那种类型的值错误:无法将浮点数NaN转换为整数。 我不知道是否可以捕获该数字并修改它以适应表单所期望的内容,或者我是否可以设置不同的形式以接受这些数字(现在它是一个forms.FloatField)。 谢谢你的帮助。
一些代码:
观点:
if request.method == 'GET':
form = ImageForm()
else:
form = ImageForm(request.POST)
... doing some cropping here then save
形式:
class ImageForm(forms.ModelForm):
class Meta:
model = Image
top_img_left = forms.FloatField(widget=forms.HiddenInput)
... then more of the same line but with middle, bottom, center, right...
这是一个字符串。
js代码:
var topZoomData = $('#img-top-remix').smoothZoom('getZoomData');
$('input[name="top_img_left"]').val(topZoomData.scaledX);
更新
我正在尝试使用toFixed函数来避免这么大的数字: http://www.w3schools.com/jsref/jsref_tofixed.asp
var topZoomData = $('#img-top-remix').smoothZoom('getZoomData');
$('input[name="top_img_left"]').val(topZoomData.scaledX).toFixed(3);