Django:使用变量翻译messages.success

时间:2018-11-02 11:33:28

标签: django

我想翻译我的view.py。通常,我是这样_("Ticket has been successfully created.")

现在,我想向其中添加一些变量。您能在我的views.py中解释我,如何翻译吗?

messages.success(self.request, f"Status of {discount.code} ticket has been successfully updated.")

1 个答案:

答案 0 :(得分:1)

在Django官方文档中,您可以找到example,如下所示:

def my_view(request, m, d):
    output = _('Today is %(month)s %(day)s.') % {'month': m, 'day': d}
    return HttpResponse(output)

因此您的情况可以是:

message = _('Status of %(discount_code)s ticket has been successfully updated.') % {'discount_code': discount.code}
messages.success(self.request, message)