Joey Coder
Joey Coder

Reputation: 3529

Django: Translate messages.success with variable

I want to translate my view.py. Usually, I did it like this _("Ticket has been successfully created.")

Now I want to add some variable to it. Can you explain me, how to translate it in that case in my views.py?

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

Upvotes: 1

Views: 795

Answers (1)

Satevg
Satevg

Reputation: 1601

In Django official documentation you can find example like this:

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

So in your case it can be:

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

Upvotes: 1

Related Questions