问题是 Django 没有添加 request.session['django_language'] = lang_code
我只是扩展语言环境中间件并使用以下代码。
def process_request(self, request):
if self.is_language_prefix_patterns_used():
lang_code = (get_language_from_path(request.path_info) or
request.session.get('django_language', settings.LANGUAGE_CODE))
activate(lang_code)
request.session['django_language'] = lang_code
request.LANGUAGE_CODE = get_language()
现在,我有以下问题。我应该怎么做才能使
http://localhost:8000不重定向(以语言代码为前缀)?
我发现以下代码:
class NoPrefixLocaleRegexURLResolver(LocaleRegexURLResolver):
@property
def regex(self):
language_code = get_language()
if language_code not in self._regex_dict:
regex_compiled = (re.compile('' % language_code, re.UNICODE)
if language_code == settings.LANGUAGE_CODE
else re.compile('^%s/' % language_code, re.UNICODE))
self._regex_dict[language_code] = regex_compiled
return self._regex_dict[language_code]
但是,该代码在检查 if 时存在问题language_code == settings.LANGUAGE_CODE
。如果我输入http://localchost:8000
,它不会重定向,而是提供一个带有翻译的页面settings.LANGUAGE_CODE
而不是request.session.get('django_language')
. 据我了解,我无法访问请求,那该怎么办?