geocoder.ip'me'在本地工作,但不在heroku上工作

问题描述

我正在使用Django,需要获取用户的当前位置,因此我使用geocoder.ip('me')。它在本地运行良好,但是在Heroku上部署它之后,似乎无法正常工作。那是因为IP地址还是类似的东西?我该如何解决?还是有什么其他方法可以让我更轻松地访问 views.py 中的用户当前位置?谢谢。

我在How do I get user IP address in django?中找到了获取IP地址的方法,但是我不知道如何真正获取IP地址,以便可以在代码的其他部分使用它。

def get_client_ip(self,request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

class RecommendView(generic.ListView):
    template_name = 'event/recommend.html'
    context_object_name = 'recommend_event_list'
    today = datetime.now().date()
    end = today + timedelta(7)
    time_start = datetime.combine(today,time())
    time_end = datetime.combine(end,time())
    ip = get_client_ip()
    myloc = geocoder.ip(ip,key='my_key')
    def get_queryset(self):
        rec=Event.objects.filter(post_date__range=(self.time_start,self.time_end))
        for event in rec:
            g = geocoder.mapbox(event.address,key='my_key')
            R = 6373.0
            lat1 = math.radians(self.myloc.lat)
            lon1 = math.radians(self.myloc.lng)
            lat2 = math.radians(g.lat)
            lon2 = math.radians(g.lng)

            dlon = lon2 - lon1
            dlat = lat2 - lat1
            a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2

            c = 2 * math.atan2(math.sqrt(a),math.sqrt(1 - a))
            dis = R * c
            if dis>20:
                rec=rec.exclude(address=event.address)
        return rec.order_by('-post_date')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...