将浮点数转换为十进制(IP 地址参数)
我正在尝试使用 GeoIP 来定位用户的位置
Class register(models.Model):
user = models.ForeignKey('auth.User', unique = True)
latitude = models.DecimalField(max_digits=12, decimal_places=10)
longitude = models.DecimalField(max_digits=12, decimal_places=10)
Availability = models.CharField(max_length=8,choices=STATUS_CHOICES, blank= False, null=False)
Status = models.CharField(max_length=50, blank = True, null= True)
,在 forms.py 中,我有
class registerForm(forms.ModelForm):
class Meta:
model=register
fields = ('Availability', 'Status')#'latitude','longitude',
def save(self,ip_address, *args, **kwargs):
g = GeoIP()
lat,lon = g.lat_lon(ip_address)
user_location = super(registerForm, self).save(commit=False)
user_location.latitude = lat
user_location.longitude = lon
user_location.save(*args, **kwargs)
,在我看来,
def status_set(request):
if request.method == "POST":
rform = registerForm(data = request.POST)
print "rejister form request"
if rform.is_valid():
register = rform.save(ip_address='119.153.117.32')
register.user=request.user
register.save(ip_address)
return render_to_response('home.html')
else:
rform = registerForm()
return render_to_response('status_set.html',{'rform':rform})
我将 IP 地址设为“119.153.117.32”,因为我正在从本地循环访问该站点,并且GeoIP 没有本地环路的经纬度 (127.0.0.1)。并且它不会为本地循环返回任何内容。\ 当我提交表单时,它显示“无法将浮点数转换为十进制。首先将浮点数转换为字符串”。我该如何解决这个问题,是否必须将其转换为字符串,或者是否有更好的方法。
im trying to locate the location of a user using GeoIP for this i have
Class register(models.Model):
user = models.ForeignKey('auth.User', unique = True)
latitude = models.DecimalField(max_digits=12, decimal_places=10)
longitude = models.DecimalField(max_digits=12, decimal_places=10)
Availability = models.CharField(max_length=8,choices=STATUS_CHOICES, blank= False, null=False)
Status = models.CharField(max_length=50, blank = True, null= True)
and in forms.py i have
class registerForm(forms.ModelForm):
class Meta:
model=register
fields = ('Availability', 'Status')#'latitude','longitude',
def save(self,ip_address, *args, **kwargs):
g = GeoIP()
lat,lon = g.lat_lon(ip_address)
user_location = super(registerForm, self).save(commit=False)
user_location.latitude = lat
user_location.longitude = lon
user_location.save(*args, **kwargs)
and in my views i have
def status_set(request):
if request.method == "POST":
rform = registerForm(data = request.POST)
print "rejister form request"
if rform.is_valid():
register = rform.save(ip_address='119.153.117.32')
register.user=request.user
register.save(ip_address)
return render_to_response('home.html')
else:
rform = registerForm()
return render_to_response('status_set.html',{'rform':rform})
i em taking the IP address as "119.153.117.32" because i am accessing the site from local loop and GeoIP do not have a lat,lon for local loop (127.0.0.1). and it returns none for local loop.\
when i submitt the form it says "Cannot convert float to Decimal. First convert the float to a string". how can i solve this issue is it compulsory to convert it to a string or is there any better way of doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 models.FloatField 作为坐标而不是 DecimalField,或者在保存对象之前使用 str(lat) 等将浮点数转换为字符串。我不确定 DecimalField 是否适合以浮点形式返回的数据,因此我更愿意更改为 FloatField,但这意味着重新同步您的数据库。
DecimalField 可能适用于 python“十进制”的内容 - 请参阅此处:
http://docs.python .org/library/decimal.html
Try using models.FloatField for your coordinates instead of DecimalField, or converting your floats to a string using str(lat) etc before saving the object. I'm not sure if DecimalField is appropriate for data that's coming back as floats so I'd prefer to change to FloatField, but that means resyncing your database.
DecimalField is probably intended for stuff that's a python 'decimal' - see here:
http://docs.python.org/library/decimal.html