在Django上设置geoip错误 [英] Error setting up geoip on Django

查看:123
本文介绍了在Django上设置geoip错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GeoIP将地理位置添加到网站。我按照 Django docs 上的说明进行操作,但是我得到了错误:不正确配置:导入中间件中间件时出错:无法导入名称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


推荐答案

导入声明应为:

from django.contrib.gis.utils.geoip import GeoIP

这篇关于在Django上设置geoip错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆