def should_redirect_with_slash(self, request):
"""
Return True if settings.APPEND_SLASH is True and appending a slash to
the request path turns an invalid path into a valid one.
"""
if settings.APPEND_SLASH and not request.get_full_path().endswith('/'):
urlconf = getattr(request, 'urlconf', None)
return (
not urlresolvers.is_valid_path(request.path_info, urlconf)
and urlresolvers.is_valid_path('%s/' % request.path_info, urlconf)
)
return False
python类is_valid_path()的实例源码
def should_redirect_with_slash(self, request):
"""
Return True if settings.APPEND_SLASH is True and appending a slash to
the request path turns an invalid path into a valid one.
"""
if settings.APPEND_SLASH and not request.get_full_path().endswith('/'):
urlconf = getattr(request, 'urlconf', None)
return (
not urlresolvers.is_valid_path(request.path_info, urlconf)
and urlresolvers.is_valid_path('%s/' % request.path_info, urlconf)
)
return False
def should_redirect_with_slash(self, request):
"""
Return True if settings.APPEND_SLASH is True and appending a slash to
the request path turns an invalid path into a valid one.
"""
if settings.APPEND_SLASH and not request.get_full_path().endswith('/'):
urlconf = getattr(request, 'urlconf', None)
return (
not urlresolvers.is_valid_path(request.path_info, urlconf)
and urlresolvers.is_valid_path('%s/' % request.path_info, urlconf)
)
return False
def process_request(request):
# ????????? ?????? ????????????? ????? GET-????????
if request.GET.get(options.MULTILANGUAGE_GET_PARAM):
request.disable_autoredirect = True
# ?????????, ??? ?? ??????? ????? ???? ????? ????????,
# ?????, ?????????? ?? MULTILANGUAGE_FALLBACK_URL
urlconf = getattr(request, 'urlconf', None)
if not is_valid_path(request.path_info, urlconf):
return redirect(options.MULTILANGUAGE_FALLBACK_URL)
return
# ????????? ?????? ????????????? ????? ????
if request.COOKIES.get(options.MULTILANGUAGE_COOKIE_KEY, None):
return
# ????????? ?????? ????????????? ??? ???????????? User-Agent
ua_string = request.META.get('HTTP_USER_AGENT', '').lower()
for pattern in options.ROBOTS_UA:
if pattern in ua_string:
return
# ????????? ?????????? ? IP
ip = get_client_ip(request)
ip_info = info(ip, detailed=True)
if not ip_info:
request.disable_autoredirect = True
return
# ??????????? ???? ?????, ?? ??????? ????? ???????????
current_code = settings.LANGUAGE_CODE
redirect_code = current_code
try:
ip_iso = ip_info.get('country').get('iso')
except AttributeError:
pass
else:
for code, opts in options.MULTILANGUAGE_SITES.items():
iso = opts.get('iso')
if iso and ip_iso in iso:
redirect_code = code
break
if current_code != redirect_code:
request.disable_autoredirect = True
language = options.MULTILANGUAGE_SITES.get(redirect_code)
if language:
redirect_url = noredirect_url(language['url'], forced_path=request.path)
return redirect(redirect_url)