3

我正在尝试使用 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
4

1 回答 1

5

看来我毕竟找到了解决方案。导入语句应该是:

from django.contrib.gis.utils.geoip import GeoIP
于 2011-02-04T11:56:05.327 回答