Error setting geoip on Django

I am trying to add geolocation to a website using GeoIP. I followed the Django docs instructions , but I get this error: ImproperlyConfigured: Error importing middleware middleware: "cannot import name GeoIP"What could be missing? I added the geolocation feature as a custom middleware, as shown below:

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
+3
source share
1 answer

I think I got the solution in the end. The import statement should be:

from django.contrib.gis.utils.geoip import GeoIP
+5
source

Source: https://habr.com/ru/post/1789960/


All Articles