本文介绍了在 Django 上设置 geoip 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 GeoIP 将地理位置添加到网站.我按照 Django docs 上的说明进行操作,但我明白了错误:ImproperlyConfigured:导入中间件中间件时出错:无法导入名称 GeoIP"
可能缺少什么?我已将地理定位功能添加为自定义中间件,如下所示:
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
这篇关于在 Django 上设置 geoip 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!