对IP进行地理编码并使用模型表单保存 [英] geocoding an IP and save using model forms

查看:95
本文介绍了对IP进行地理编码并使用模型表单保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的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)
可用性= models.CharField(max_length = 8,choices = STATUS_CHOICES,blank = False,null = False)
状态= models.CharField(max_length = 50,blank = True,null = True)
pre>

并且在forms.py我有

  class registerForm form.ModelForm)

class Meta:
model = register
fields =('latitude','longitude','Availability','Status')

现在我想要的是对地理编码用户的IP地址进行地理编码后自动获取纬度和经度并将其保存到数据库。我不想允许用户手动输入纬度和经度,因为它将是一个奇怪的,绝对没有人会喜欢手动进行。
我正在使用GeoIP来对IP地址进行地理编码。在我的views.py我有

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

我正在寻找一种自动获得拉特,然后从GeoIP中将其放置在纬度经度字段中,以便用户无需手动输入。然后输入状态和可用性后,表单可以保存到数据库。
任何帮助将不胜感激

解决方案

覆盖表单的保存方式以填充纬度&经度

  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)


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

解决方案

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)

这篇关于对IP进行地理编码并使用模型表单保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆