为我的网站设置geoip时出现错误“ GeoIP路径必须是有效的文件或目录”, [英] Error while setting up geoip for my site "GeoIP path must be a valid file or directory"

查看:204
本文介绍了为我的网站设置geoip时出现错误“ GeoIP路径必须是有效的文件或目录”,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的django应用程序,我试图通过amdin存储登录位置的日志。



我创建了一个中间件,并尝试从 django.contrib.gis.utils import GeoIP中使用它来获取地理位置的纬度和经度值。



但是出现类似以下错误:



位于/ admin / 的GeoPException p>

GeoIP路径必须是有效的文件或目录。



代码: trackware.py

 从django.contrib.gis.utils导入GeoIP 
从core.models导入Log#您的简单Log模型

def get_ip(request):
xff = request.META.get('HTTP_X_FORWARDED_FOR')
if xff:
return xff.split(',')[ 0]
返回request.META.get('REMOTE_ADDR')

class UserLocationLoggerMiddleware(object):

def process_request(self,request):
如果request.user和request.user.is_superuser:
#仅记录超级用户的请求,
#您可以通过添加设置
#来跟踪其他用户类型
来控制它ip = get_ip(请求)
g = GeoIP()
纬度,经度= g.lat_lon(ip)
Log.o bjects.create(request.user,ip,lat,long)

我在中进行的更改> settings.py:

  MIDDLEWARE_CLASSES =(
'django.middleware.common.CommonMiddleware',

#自定义中间件
'smartDNA.trackware.UserLocationLoggerMiddleware',


BASE_DIR = os.path.abspath(os.path.dirname( __file__))
GEOIP_PATH = os.path.join(BASE_DIR,'geoip')

models.py:

  class Log(models.Model):
用户=模型.CharField(verbose_name = User,max_length = 32)
ip = models.IPAddressField(verbose_name = IP)
lat = models.DecimalField(verbose_name = Lat,max_digits = 10,decimal_places = 1)
long = models.DecimalField(verbose_name = Long,max_digits = 10,decimal_places = 1)

admin.py中注册模型:

  admin.site.register(日志)

我正在做的错误是什么,请解决......

解决方案

文档


为了执行基于IP的地理定位,GeoIP对象需要
个GeoIP C库以及$ b $中的GeoIP国家或城市数据集b二进制格式(CSV文件将不起作用!)。 这些数据集可能是从MaxMind下载的
。抓取GeoLiteCountry / GeoIP.dat.gz和
GeoLiteCity.dat.gz文件,并将其解压缩到与您在设置中设置GEOIP_PATH的
相对应的目录中。


请确保已完成上述操作,否则系统不知道如何将IP映射到位置。


For my django app I am trying to store log of login location by amdin.

I created a middleware and trying use from "django.contrib.gis.utils import GeoIP" to get the latitude and longitude values of geo locations.

But getting error something like:

GeoIPException at /admin/

GeoIP path must be a valid file or directory.

code: trackware.py

from django.contrib.gis.utils import GeoIP
from core.models import Log # your simple Log model

def get_ip(request):
   xff = request.META.get('HTTP_X_FORWARDED_FOR')
   if xff:
      return xff.split(',')[0]
   return request.META.get('REMOTE_ADDR')

class UserLocationLoggerMiddleware(object):

    def process_request(self, request):
        if request.user and request.user.is_superuser:
            # Only log requests for superusers,
            # you can control this by adding a setting
            # to track other user types
            ip = get_ip(request)
            g = GeoIP()
            lat,long = g.lat_lon(ip)
            Log.objects.create(request.user, ip, lat, long)

changes that I made in settings.py:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',

#custom middleware
    'smartDNA.trackware.UserLocationLoggerMiddleware',
)

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
GEOIP_PATH =os.path.join(BASE_DIR, 'geoip')

models.py:

class Log(models.Model):
    user = models.CharField(verbose_name="User",max_length=32)
    ip=models.IPAddressField(verbose_name="IP")
    lat= models.DecimalField(verbose_name="Lat",max_digits=10,decimal_places=1)
    long= models.DecimalField(verbose_name="Long",max_digits=10,decimal_places=1)

register model in admin.py:

admin.site.register(Log)

What is the mistake that I am doing and solution please....

解决方案

From the documentation:

In order to perform IP-based geolocation, the GeoIP object requires the GeoIP C libary and either the GeoIP Country or City datasets in binary format (the CSV files will not work!). These datasets may be downloaded from MaxMind. Grab the GeoLiteCountry/GeoIP.dat.gz and GeoLiteCity.dat.gz files and unzip them in a directory corresponding to what you set GEOIP_PATH with in your settings.

Make sure you have done the above, otherwise the system does not know how to map IPs to locations.

这篇关于为我的网站设置geoip时出现错误“ GeoIP路径必须是有效的文件或目录”,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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