Python gettext error: cannot convert __proxy__ object to str implicitly

I suddenly get a strange error in code that previously worked. I recently upgraded to Django 1.9.6 from version 1.9.4.

In one of my views, I have:

from django.contrib import messages from django.utils.translation import ugettext_lazy as _ messages.success(request, str( _('A string with a ') + '<a target="_blank" href="/preview/' + mymodel.hash + '">' + _('link!') + '</a>.'), extra_tags="safehtml" ) 

Now this gives a TypeError on the second last line:

 Can't convert '__proxy__' object to str implicitly 

Why? How to fix it?

Edit:

This can be fixed by transferring the second call to ugettext_lazy() to str() (i.e. the code becomes str( _('link!') ) . This allows the presentation to be rendered perfectly. Now my questions are: Why? The whole composite line is already wrapped in str() , and as I said, this code worked fine with the previous version of django. Is this a bug?

+5
source share
1 answer

__proxy__ is a translation string whose actual translation result is not defined until the object is used in the string (that is, what happens when you use ugettext_lazy instead of ugettext here).

Documentation

+3
source

All Articles