MCPcopy
hub / github.com/django/django / set_language

Function set_language

django/views/i18n.py:30–74  ·  view source on GitHub ↗

Redirect to a given URL while setting the chosen language in the language 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 cal

(request)

Source from the content-addressed store, hash-verified

28
29
30def set_language(request):
31 """
32 Redirect to a given URL while setting the chosen language in the language
33 cookie. The URL and the language code need to be specified in the request
34 parameters.
35
36 Since this view changes how the user will see the rest of the site, it must
37 only be accessed as a POST request. If called as a GET request, it will
38 redirect to the page in the request (the 'next' parameter) without changing
39 any state.
40 """
41 next_url = request.POST.get("next", request.GET.get("next"))
42 if (
43 next_url or request.accepts("text/html")
44 ) and not url_has_allowed_host_and_scheme(
45 url=next_url,
46 allowed_hosts={request.get_host()},
47 require_https=request.is_secure(),
48 ):
49 next_url = request.META.get("HTTP_REFERER")
50 if not url_has_allowed_host_and_scheme(
51 url=next_url,
52 allowed_hosts={request.get_host()},
53 require_https=request.is_secure(),
54 ):
55 next_url = "/"
56 response = HttpResponseRedirect(next_url) if next_url else HttpResponse(status=204)
57 if request.method == "POST":
58 lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER)
59 if lang_code and check_for_language(lang_code):
60 if next_url:
61 next_trans = translate_url(next_url, lang_code)
62 if next_trans != next_url:
63 response = HttpResponseRedirect(next_trans)
64 response.set_cookie(
65 settings.LANGUAGE_COOKIE_NAME,
66 lang_code,
67 max_age=settings.LANGUAGE_COOKIE_AGE,
68 path=settings.LANGUAGE_COOKIE_PATH,
69 domain=settings.LANGUAGE_COOKIE_DOMAIN,
70 secure=settings.LANGUAGE_COOKIE_SECURE,
71 httponly=settings.LANGUAGE_COOKIE_HTTPONLY,
72 samesite=settings.LANGUAGE_COOKIE_SAMESITE,
73 )
74 return response
75
76
77def get_formats():

Callers

nothing calls this directly

Calls 10

HttpResponseClass · 0.90
check_for_languageFunction · 0.90
translate_urlFunction · 0.90
acceptsMethod · 0.80
get_hostMethod · 0.80
getMethod · 0.45
is_secureMethod · 0.45
set_cookieMethod · 0.45

Tested by

no test coverage detected