summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2017-12-02 13:00:30 +0100
committerTim Graham <timograham@gmail.com>2018-01-03 11:25:40 -0500
commitb3cd9fb18ba127c067a4f3c79a32e51c8d8cc6ae (patch)
tree9895f2f119b7636d0a8ab1c945745bf97c994bba
parentccc25bfe4f0964a00df3af6f91c2d9e20159a0c2 (diff)
Refs #15902 -- Made set_language() view always set the current language in a cookie.
The plan is to later deprecate/remove storing the language in the session.
-rw-r--r--django/views/i18n.py19
-rw-r--r--docs/topics/i18n/translation.txt12
-rw-r--r--tests/view_tests/tests/test_i18n.py6
3 files changed, 23 insertions, 14 deletions
diff --git a/django/views/i18n.py b/django/views/i18n.py
index e121d3038a..1f3170f71f 100644
--- a/django/views/i18n.py
+++ b/django/views/i18n.py
@@ -22,9 +22,9 @@ LANGUAGE_QUERY_PARAMETER = 'language'
def set_language(request):
"""
- Redirect to a given URL while setting the chosen language in the session or
- cookie. The URL and the language code need to be specified in the request
- parameters.
+ Redirect to a given URL while setting the chosen language in the session
+ (if enabled) and in a cookie. The URL and the language code need to be
+ specified in the request parameters.
Since this view changes how the user will see the rest of the site, it must
only be accessed as a POST request. If called as a GET request, it will
@@ -49,13 +49,12 @@ def set_language(request):
response = HttpResponseRedirect(next_trans)
if hasattr(request, 'session'):
request.session[LANGUAGE_SESSION_KEY] = lang_code
- else:
- response.set_cookie(
- settings.LANGUAGE_COOKIE_NAME, lang_code,
- max_age=settings.LANGUAGE_COOKIE_AGE,
- path=settings.LANGUAGE_COOKIE_PATH,
- domain=settings.LANGUAGE_COOKIE_DOMAIN,
- )
+ response.set_cookie(
+ settings.LANGUAGE_COOKIE_NAME, lang_code,
+ max_age=settings.LANGUAGE_COOKIE_AGE,
+ path=settings.LANGUAGE_COOKIE_PATH,
+ domain=settings.LANGUAGE_COOKIE_DOMAIN,
+ )
return response
diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt
index 2bdf40fea1..0c5121b79c 100644
--- a/docs/topics/i18n/translation.txt
+++ b/docs/topics/i18n/translation.txt
@@ -1761,10 +1761,14 @@ Activate this view by adding the following line to your URLconf::
language-independent itself to work correctly.
The view expects to be called via the ``POST`` method, with a ``language``
-parameter set in request. If session support is enabled, the view
-saves the language choice in the user's session. Otherwise, it saves the
-language choice in a cookie that is by default named ``django_language``.
-(The name can be changed through the :setting:`LANGUAGE_COOKIE_NAME` setting.)
+parameter set in request. If session support is enabled, the view saves the
+language choice in the user's session. It also saves the language choice in a
+cookie that is named ``django_language`` by default. (The name can be changed
+through the :setting:`LANGUAGE_COOKIE_NAME` setting.)
+
+.. versionchanged:: 2.1
+
+ In older versions, the cookie is only set if session support isn't enabled.
After setting the language choice, Django looks for a ``next`` parameter in the
``POST`` or ``GET`` data. If that is found and Django considers it to be a safe
diff --git a/tests/view_tests/tests/test_i18n.py b/tests/view_tests/tests/test_i18n.py
index 38e5367d03..125d6e0a5f 100644
--- a/tests/view_tests/tests/test_i18n.py
+++ b/tests/view_tests/tests/test_i18n.py
@@ -37,6 +37,12 @@ class SetLanguageTests(TestCase):
response = self.client.post('/i18n/setlang/', post_data, HTTP_REFERER='/i_should_not_be_used/')
self.assertRedirects(response, '/')
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)
+ # The language is set in a cookie.
+ language_cookie = self.client.cookies[settings.LANGUAGE_COOKIE_NAME]
+ self.assertEqual(language_cookie.value, lang_code)
+ self.assertEqual(language_cookie['domain'], '')
+ self.assertEqual(language_cookie['path'], '/')
+ self.assertEqual(language_cookie['max-age'], '')
def test_setlang_unsafe_next(self):
"""