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

查看:97
本文介绍了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 ?

谢谢

编辑:我想要城市

推荐答案

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

文档非常详细地解释事情;我会花点时间彻底阅读他们。

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

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

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