我的网站不是英文版,但我希望Django管理员只能用英文显示。
无论LANGUAGE_CODE
设置如何,我如何强制Django管理员以英语显示?
答案 0 :(得分:5)
据我所知,此功能没有开箱即用的设置,但您可以轻松地在中间件中对其进行编码。将此代码添加到您的某个应用程序的middleware.py文件中:
from django.utils import translation
class AdminLocaleMiddleware(object):
def process_request(self, request):
if request.path.startswith('/admin'):
translation.activate("en")
request.LANGUAGE_CODE = translation.get_language()
在LocaleMiddleware:
之后将AdminLocaleMiddleware添加到您的设置中MIDDLEWARE_CLASSES = [
...
"django.middleware.locale.LocaleMiddleware",
"your_app.middleware.AdminLocaleMiddleware",
...
]
答案 1 :(得分:1)
对于 Django 2.0 :
from django.utils import translation
class AdminLocaleMiddleware:
def __init__(self, process_request):
self.process_request = process_request
def __call__(self, request):
if request.path.startswith('/admin'):
translation.activate("en")
request.LANGUAGE_CODE = translation.get_language()
response = self.process_request(request)
return response