Django,检索 IP 位置 [英] Django, Retrieve IP location

查看:27
本文介绍了Django,检索 IP 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从他们的 IP 地址检测他们的位置来将我的用户重定向到我网站中的特定位置区域.

I would like to redirect my users to specific location areas in my website, by detecting their location from their IP address.

在 Django 1.1.1 下实现这一目标的最佳方法是什么?

What would be the best way to achieve this under Django 1.1.1 ?

谢谢

我想要欧洲的城市定位.

I want city based locationing on europe.

推荐答案

GeoDjango 看起来它会满足您的需求.我不确定您希望如何引导用户,但使用 GeoIP API,您可以执行以下操作:

GeoDjango looks like it will suit your needs. I'm not sure exactly how you would want to direct users, but using the GeoIP API, you can do something like:

from django.contrib.gis.utils import GeoIP
g = GeoIP()
ip = request.META.get('REMOTE_ADDR', None)
if ip:
    city = g.city(ip)['city']
else:
    city = 'Rome' # default city

# proceed with city

Docs 解释得很详细;我会花点时间彻底阅读它们.

The Docs explain things in great detail; I would take a moment to read through them thoroughly.

这篇关于Django,检索 IP 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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