在django中间件中使用HTTP HttpResponsePermantRedirect时,站点进入重定向循环
当我尝试在Heroku上部署时,我在我的网站上得到了带有此代码的重定向循环。下面是中间件的代码在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
# -*- 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
非常感谢!你太棒了:)很高兴能帮上忙:)