MCPcopy
hub / github.com/django/django / process_response

Method process_response

django/middleware/locale.py:38–80  ·  view source on GitHub ↗
(self, request, response)

Source from the content-addressed store, hash-verified

36 request.LANGUAGE_CODE = translation.get_language()
37
38 def process_response(self, request, response):
39 language = translation.get_language()
40 language_from_path = translation.get_language_from_path(request.path_info)
41 urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
42 (
43 i18n_patterns_used,
44 prefixed_default_language,
45 ) = is_language_prefix_patterns_used(urlconf)
46
47 if (
48 response.status_code == 404
49 and not language_from_path
50 and i18n_patterns_used
51 and prefixed_default_language
52 ):
53 # Maybe the language code is missing in the URL? Try adding the
54 # language prefix and redirecting to that URL.
55 language_path = "/%s%s" % (language, request.path_info)
56 path_valid = is_valid_path(language_path, urlconf)
57 path_needs_slash = not path_valid and (
58 settings.APPEND_SLASH
59 and not language_path.endswith("/")
60 and is_valid_path("%s/" % language_path, urlconf)
61 )
62
63 if path_valid or path_needs_slash:
64 script_prefix = get_script_prefix()
65 # Insert language after the script prefix and before the
66 # rest of the URL
67 language_url = request.get_full_path(
68 force_append_slash=path_needs_slash
69 ).replace(script_prefix, "%s%s/" % (script_prefix, language), 1)
70 # Redirect to the language-specific URL as detected by
71 # get_language_from_request(). HTTP caches may cache this
72 # redirect, so add the Vary header.
73 redirect = self.response_redirect_class(language_url)
74 patch_vary_headers(redirect, ("Accept-Language", "Cookie"))
75 return redirect
76
77 if not (i18n_patterns_used and language_from_path):
78 patch_vary_headers(response, ("Accept-Language",))
79 response.headers.setdefault("Content-Language", language)
80 return response

Callers

nothing calls this directly

Calls 6

is_valid_pathFunction · 0.90
get_script_prefixFunction · 0.90
patch_vary_headersFunction · 0.90
get_full_pathMethod · 0.80
setdefaultMethod · 0.45

Tested by

no test coverage detected