2014-02-05 8 views
0

Heroku에서 배포하려고하면 웹 사이트에서이 코드로 리디렉션 루프가 발생합니다. 다음은 미들웨어 코드입니다.django 미들웨어에서 HTTP HttpResponsePermanentRedirect를 사용할 때 사이트가 리디렉션 루프로 들어갑니다.

# -*- 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 

어디서 잘못했는지 제안하고 더 나은 방법이 있는지 제안하십시오.

미리 감사드립니다.

답변

1

인도와 네덜란드 사람들은 항상 방향을 바꾸고 있습니다. 아무런 문제가 없기 때문에 루프가 있습니다. request.path/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 
+0

감사합니다. 당신은 끝내줍니다 :) –

+0

도움이되기를 기쁘게 생각합니다 :) – tayfun