对 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)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
覆盖表单的保存方法以填充纬度和纬度经度
Overwrite the save method of form to populate latitude & longitude
将此行更改
为
change this line
to