Fixed #19277 -- Added LocaleMiddleware.response_redirect_class
Thanks ppetrid at yawd.eu for the suggestion.
This commit is contained in:
parent
fa2e1371cd
commit
7a97df190c
|
@ -18,6 +18,7 @@ class LocaleMiddleware(object):
|
|||
translated to the language the user desires (if the language
|
||||
is available, of course).
|
||||
"""
|
||||
response_redirect_class = HttpResponseRedirect
|
||||
|
||||
def __init__(self):
|
||||
self._supported_languages = OrderedDict(settings.LANGUAGES)
|
||||
|
@ -52,7 +53,7 @@ class LocaleMiddleware(object):
|
|||
language_url = "%s://%s/%s%s" % (
|
||||
'https' if request.is_secure() else 'http',
|
||||
request.get_host(), language, request.get_full_path())
|
||||
return HttpResponseRedirect(language_url)
|
||||
return self.response_redirect_class(language_url)
|
||||
|
||||
# Store language back into session if it is not present
|
||||
if hasattr(request, 'session'):
|
||||
|
|
|
@ -151,6 +151,12 @@ Enables language selection based on data from the request. It customizes
|
|||
content for each user. See the :doc:`internationalization documentation
|
||||
</topics/i18n/translation>`.
|
||||
|
||||
.. attribute:: LocaleMiddleware.response_redirect_class
|
||||
|
||||
Defaults to :class:`~django.http.HttpResponseRedirect`. Subclass
|
||||
``LocaleMiddleware`` and override the attribute to customize the redirects
|
||||
issued by the middleware.
|
||||
|
||||
Message middleware
|
||||
------------------
|
||||
|
||||
|
|
|
@ -267,6 +267,12 @@ Forms
|
|||
:func:`~django.forms.formsets.formset_factory` to allow validating
|
||||
a minimum number of submitted forms.
|
||||
|
||||
Internationalization
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
* The :attr:`django.middleware.locale.LocaleMiddleware.response_redirect_class`
|
||||
attribute allows you to customize the redirects issued by the middleware.
|
||||
|
||||
Management Commands
|
||||
^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ import os
|
|||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.urlresolvers import reverse, clear_url_caches
|
||||
from django.http import HttpResponsePermanentRedirect
|
||||
from django.middleware.locale import LocaleMiddleware
|
||||
from django.test import TestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.template import Template, Context
|
||||
|
@ -11,6 +13,10 @@ from django.utils._os import upath
|
|||
from django.utils import translation
|
||||
|
||||
|
||||
class PermanentRedirectLocaleMiddleWare(LocaleMiddleware):
|
||||
response_redirect_class = HttpResponsePermanentRedirect
|
||||
|
||||
|
||||
@override_settings(
|
||||
USE_I18N=True,
|
||||
LOCALE_PATHS=(
|
||||
|
@ -181,6 +187,16 @@ class URLRedirectTests(URLTestCaseBase):
|
|||
response = self.client.get(response['location'])
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
@override_settings(
|
||||
MIDDLEWARE_CLASSES=(
|
||||
'i18n.patterns.tests.PermanentRedirectLocaleMiddleWare',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
),
|
||||
)
|
||||
def test_custom_redirect_class(self):
|
||||
response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='en')
|
||||
self.assertRedirects(response, '/en/account/register/', 301)
|
||||
|
||||
|
||||
class URLVaryAcceptLanguageTests(URLTestCaseBase):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue