Reputation: 6319
I'm trying to add geolocation to a website, using GeoIP. I followed the instructions on Django docs, but I get this error: ImproperlyConfigured: Error importing middleware middleware: "cannot import name GeoIP"
What can be missing? I've added the geolocation function as a custom middleware as 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
Upvotes: 3
Views: 2287
Reputation: 6319
Seems I got a solution after all. The import statement should be:
from django.contrib.gis.utils.geoip import GeoIP
Upvotes: 5