在 Django 上设置 geoip 时出错
我正在尝试使用 GeoIP 将地理定位添加到网站。我按照 Django 文档 上的说明进行操作,但我明白了错误:不正确配置:导入中间件时出错 中间件:“无法导入名称 GeoIP”
可能缺少什么?我已将地理定位功能添加为自定义中间件,如下所示:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来我终于找到了解决方案。导入声明应该是:
Seems I got a solution after all. The import statement should be: