此网页有重定向循环?

时间:2015-08-31 19:12:26

标签: python django geoip

我想根据请求的IP将请求重定向到相应的URL。为此,我正在编写一个中间件。

from django.contrib.gis.geoip import GeoIP
from teashop import settings as settings
from django.http import HttpResponseRedirect

# IPs for Testing
# Indian IP : '182.75.107.138'
# Russian IP : '95.108.142.138'
# Chinese IP : '27.227.255.255'


class GeoRedirectionMiddleware(object):


    def process_request(self, request):
        """
        Detect Location using GeoIP
        """ 
        g = GeoIP()
        ip = '95.108.142.138'
        country_dict = g.country(ip)
        if country_dict["country_code"] == "RU":
           if '/ru' not in request.path:
              return HttpResponseRedirect('/ru'+request.path)
           else:
              pass

这会将'/'重定向到'/ru/'。但是,当我访问其他网址'/abc'时,它会显示太多重定向。

我的中间件

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'oscar.apps.basket.middleware.BasketMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'forked_apps.middleware.GeoRedirectionMiddleware',
)

0 个答案:

没有答案