我可以将例外的消息语言更改为其他语言吗?



我可以将异常的消息语言更改为中文吗?

try:
    new_project = admin_conn.conn.identity.create_project(**project_params)  # create_project
    print (new_project, new_project.id, "new project created")
except Exception as e:
    print (e.message)
    render(request, '/register/', {"errors": e.message.encode('utf-8')})

e.message.encode('utf-8')是英语,如何将其更改为中文?


编辑

由于异常的消息有许多类型错误,所以我不能转换每个错误,因此在Django中是否还有其他方便的方法可以做到这一点?

是的,您可以使用internationalization package i18n

,然后放置您的内容b/w {% trans %}标签。

确保在项目设置中确保

USE_I18N = True

在html

{% load i18n %}
{% trans "Successfully landed on this page" %}

如果您有message变量。

{% trans {{message}} %}

不要foget chineese添加为您的语言。

最新更新