Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在django中间件中使用HTTP HttpResponsePermantRedirect时,站点进入重定向循环_Django_Heroku_Geoip_Django Middleware - Fatal编程技术网

在django中间件中使用HTTP HttpResponsePermantRedirect时,站点进入重定向循环

在django中间件中使用HTTP HttpResponsePermantRedirect时,站点进入重定向循环,django,heroku,geoip,django-middleware,Django,Heroku,Geoip,Django Middleware,当我尝试在Heroku上部署时,我在我的网站上得到了带有此代码的重定向循环。下面是中间件的代码 # -*- coding: utf-8 -*- from django.utils.functional import SimpleLazyObject from django.conf import settings from django.http import HttpResponsePermanentRedirect def get_country_request(ip): impo

当我尝试在Heroku上部署时,我在我的网站上得到了带有此代码的重定向循环。下面是中间件的代码

# -*- coding: utf-8 -*-

from django.utils.functional import SimpleLazyObject
from django.conf import settings
from django.http import HttpResponsePermanentRedirect

def get_country_request(ip):
   import pygeoip
   file_path = settings.PROJECT_ROOT + '/data/GeoIP.dat.dat'
   gi = pygeoip.GeoIP(file_path)
   country = gi.country_name_by_addr(ip)   
   if country:
      return country

class LocationMiddleWare(object):


    def process_request(self, request):
        if 'HTTP_X_FORWARDED_FOR' in request.META:
            request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR']
        ip = request.META['REMOTE_ADDR']
        print request.path
        country = get_country_request(ip)             
        if country == "India":
          return HttpResponsePermanentRedirect('/en/')       
        if country == "Netherlands":
          return HttpResponsePermanentRedirect('/nl/')
        return None
请说明我哪里做错了,并说明是否有更好的方法


提前谢谢

你总是在重新引导来自印度和荷兰的人,这是一个循环,因为没有中断。仅当
请求时才应执行重定向。路径
不是
/en/
/nl/

def process_request(self, request):
    # NOTICE: This will make sure redirect loop is broken.
    if request.path in ["/en/", "/nl/"]:
        return None
    if 'HTTP_X_FORWARDED_FOR' in request.META:
        request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR']
    ip = request.META['REMOTE_ADDR']
    print request.path
    country = get_country_request(ip)             
    if country == "India":
        return HttpResponsePermanentRedirect('/en/')       
    if country == "Netherlands":
        return HttpResponsePermanentRedirect('/nl/')
    return None

非常感谢!你太棒了:)很高兴能帮上忙:)