0

我正在尝试检索访问者的位置。成功检索 IP 地址后,我想使用 GeoIP2 对象来获取有关位置的信息。 https://docs.djangoproject.com/en/2.2/ref/contrib/gis/geoip2/#django.contrib.gis.geoip2.GeoIP2

在我的 settings.py 文件中,我将“django.contrib.gis.geoip2”添加到我安装的应用程序中:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis.geoip2',
    'web'
]

使用 shell 一切正常 [python3 manage.py shell]:

目录(django.contrib.gis.geoip2)

['GeoIP2', 'GeoIP2Exception', 'HAS_GEOIP2', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'base', 'geoip2', 'resources']

但是,尝试在我的应用程序“web”中使用 GeoIP2 对象时出现错误:“django.contrib.gis.geoip2 没有属性 GeoIP2”。

['HAS_GEOIP2', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
4

1 回答 1

0

您链接到的文档中提到了一个依赖项,您的 Web 服务器上缺少该依赖项:

为了执行基于 IP 的地理定位,GeoIP2 对象需要geoip2 Python 库

您需要使用pip install geoip2.

请注意,文档还提到了您还需要设置才能使用此模块的其他要求。

于 2019-06-09T04:24:33.183 回答