Error setting up geoip on Django
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
Seems I got a solution after all. The import statement should be:
from django.contrib.gis.utils.geoip import GeoIP
精彩评论