对 IP 进行地理编码并使用模型表单保存

发布于 2024-10-04 02:50:49 字数 1321 浏览 6 评论 0原文

我的 models.py 文件

        user = models.ForeignKey('auth.User', unique = True) 
        latitude = models.DecimalField(max_digits=8, decimal_places=6)
        longitude = models.DecimalField(max_digits=8, decimal_places=6)
        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 = ('latitude', 'longitude', 'Availability', 'Status')

有这个东西,我现在想要的是对用户的 IP 地址进行地理编码,并在对 IP 进行地理编码后自动获取纬度和经度并将其保存到数据库中。我不想允许用户手动输入纬度和经度,因为这会很奇怪,而且肯定没有人愿意手动输入。 我正在使用 GeoIP 对 IP 地址进行地理编码。在我的views.py中,我

def Userlocation(request):
    if request.method == "POST":
        rform = registerForm(data = request.POST)
        if rform.is_valid():
            register = rform.save(commit=False)
            register.user=request.user
            register.save()
            return render_to_response('home.html')
    else:
        rform = registerForm() 
    return render_to_response('status_set.html',{'rform':rform}) 

正在寻找一种方法来自动从GeoIP获取纬度,经度并将其放置在表单中的纬度经度字段中,以便用户不必手动输入它。然后输入状态和可用性后,可以将表格保存到数据库中。 任何帮助将不胜感激

i have this thing in my models.py file

        user = models.ForeignKey('auth.User', unique = True) 
        latitude = models.DecimalField(max_digits=8, decimal_places=6)
        longitude = models.DecimalField(max_digits=8, decimal_places=6)
        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 = ('latitude', 'longitude', 'Availability', 'Status')

now what i want here is to geocode the Ip address of the user and get the latitude and longitude automatically after geocoding the IP and save it to the database. i dont want to allow the user to enter the latitide and longitude manually as it will be an odd one and definately nobody will like to do it manually.
i am using GeoIP to geocode the IP address. and in my views.py i have

def Userlocation(request):
    if request.method == "POST":
        rform = registerForm(data = request.POST)
        if rform.is_valid():
            register = rform.save(commit=False)
            register.user=request.user
            register.save()
            return render_to_response('home.html')
    else:
        rform = registerForm() 
    return render_to_response('status_set.html',{'rform':rform}) 

i am looking for a way to automatically get the lat,lon from the GeoIP and place it in the latitude longitude field in forms so that the users do not have to manually enter it. and then after entering the status and availability the forms can be saved to database.
any help would be greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

弄潮 2024-10-11 02:50:49

覆盖表单的保存方法以填充纬度和纬度经度

from django.contrib.gis.utils import GeoIP
class registerForm(forms.ModelForm): 
    class Meta:
        model=register
        fields = ('Availability', 'Status')

    def save(self, ip_address, *args, **kwargs):
        g = GeoIP()
        lat, lon = `get the lat & lng`
        user_location = super(registerForm, self).save(commit=False)
        user_location.latitude = lat
        user_location.longitude = lon
        user_location.save(*args, **kwargs)

Overwrite the save method of form to populate latitude & longitude

from django.contrib.gis.utils import GeoIP
class registerForm(forms.ModelForm): 
    class Meta:
        model=register
        fields = ('Availability', 'Status')

    def save(self, ip_address, *args, **kwargs):
        g = GeoIP()
        lat, lon = `get the lat & lng`
        user_location = super(registerForm, self).save(commit=False)
        user_location.latitude = lat
        user_location.longitude = lon
        user_location.save(*args, **kwargs)
開玄 2024-10-11 02:50:49

将此行更改

register = rform.save(commit=False)

register = rform.save(ip_address=request.META['REMOTE_ADDR'])

change this line

register = rform.save(commit=False)

to

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