django request.META 获取用户真实地址

2020-05-09 16:11:44 浏览数 (1)

代码语言:javascript复制
#views.py
class V1_sou(APIView):
    def get(self,request):
        name=request.GET.get('name')
        # 对查询的结果去重
        # 判断是否有代理,以便于获取真实ip,而不是nginx所代理的本地ip
        if request.META.get('HTTP_X_FORWARDED_FOR'):
            ip = request.META.get("HTTP_X_FORWARDED_FOR")
        else:
            ip = request.META.get("REMOTE_ADDR")

        print("ip : ", ip)
        a = models.Movies.objects.filter(名字__contains=f'{name}').distinct()
        print(a)
        ps = V1s(a, many=True)
        print(ps)
        dat={}
        dat['count']=len(a)
        dat['restful']=ps.data
        return Response(dat)

0 人点赞