HaltingProblem
HaltingProblem

Reputation: 1

How to fix "django.contrib.gis.geoip2 has no attribute GeoIP2"

I am trying to retrieve my visitors' location. After successfully retrieving the IP Adress I want to use the GeoIP2 object to get information about the location. https://docs.djangoproject.com/en/2.2/ref/contrib/gis/geoip2/#django.contrib.gis.geoip2.GeoIP2

In my settings.py file I added 'django.contrib.gis.geoip2' to my installed apps:

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'
]

Using the shell everything works perfect [python3 manage.py shell]:

dir(django.contrib.gis.geoip2)

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

However trying to use the GeoIP2 object in my application 'web' I get the error: "django.contrib.gis.geoip2 has no attribute GeoIP2".

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

Upvotes: 0

Views: 473

Answers (1)

solarissmoke
solarissmoke

Reputation: 31514

There is a dependency mentioned in the documentation you link to, which you are missing on your web server:

In order to perform IP-based geolocation, the GeoIP2 object requires the geoip2 Python library

You need to install this with pip install geoip2.

Note that the documentation also mentions other requirements which you also need to set in order to use this module.

Upvotes: 0

Related Questions