【发布时间】:2011-02-04 10:34:30
【问题描述】:
我正在尝试使用 GeoIP 将地理位置添加到网站。我按照Django docs 上的说明进行操作,但出现此错误:ImproperlyConfigured: Error importing middleware middleware: "cannot import name GeoIP" 可能缺少什么?我已将地理定位功能添加为自定义中间件,如下所示:
from django.contrib.gis.utils import GeoIP
class LocationMiddleware(object):
def process_request(self, request):
g = GeoIP()
ip = request.META.get('REMOTE_ADDR', None)
if (not ip or ip == '127.0.0.1') and
request.META.has_key('HTTP_X_FORWARDED_FOR'):
ip = request.META['HTTP_X_FORWARDED_FOR']
if ip:
city = g.city(ip)['city']
else:
# set default city
return city
【问题讨论】:
标签: django geolocation geoip