将浮点数转换为十进制(IP 地址参数)

发布于 2024-10-07 05:52:51 字数 1707 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-10-14 05:52:51

尝试使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文