我正在尝试使用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
答案 0 :(得分:5)
似乎我得到了一个解决方案。 import语句应为:
from django.contrib.gis.utils.geoip import GeoIP