mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #17734 -- Made sure to only redirect translated URLs if they can actually be resolved to prevent unwanted redirects. Many thanks to Orne Brocaar and Anssi Kääriäinen for input.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17621 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
"This is the locale selecting middleware that will look at accept headers"
|
||||
|
||||
from django.core.urlresolvers import get_resolver, LocaleRegexURLResolver
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import (is_valid_path, get_resolver,
|
||||
LocaleRegexURLResolver)
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.utils.cache import patch_vary_headers
|
||||
from django.utils import translation
|
||||
|
||||
|
||||
class LocaleMiddleware(object):
|
||||
"""
|
||||
This is a very simple middleware that parses a request
|
||||
@@ -23,13 +26,17 @@ class LocaleMiddleware(object):
|
||||
|
||||
def process_response(self, request, response):
|
||||
language = translation.get_language()
|
||||
translation.deactivate()
|
||||
|
||||
if (response.status_code == 404 and
|
||||
not translation.get_language_from_path(request.path_info)
|
||||
and self.is_language_prefix_patterns_used()):
|
||||
return HttpResponseRedirect(
|
||||
'/%s%s' % (language, request.get_full_path()))
|
||||
urlconf = getattr(request, 'urlconf', None)
|
||||
language_path = '/%s%s' % (language, request.path_info)
|
||||
if settings.APPEND_SLASH and not language_path.endswith('/'):
|
||||
language_path = language_path + '/'
|
||||
if is_valid_path(language_path, urlconf):
|
||||
return HttpResponseRedirect(
|
||||
'/%s%s' % (language, request.get_full_path()))
|
||||
translation.deactivate()
|
||||
|
||||
patch_vary_headers(response, ('Accept-Language',))
|
||||
if 'Content-Language' not in response:
|
||||
|
||||
Reference in New Issue
Block a user