#34886 closed Bug (fixed)
Sample use of lazy in delayed translations is not valid in Django 4.1 and 4.2.
Reported by: | Stefan Hammer | Owned by: | Lufafa Joshua |
---|---|---|---|
Component: | Documentation | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Claude Paroz | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The sample code to mark a lazy translation as "safe" in the docs here seems to be invalid since Django 4.1 (#20296).
We're in the process of upgrading a Django 3.2 installation to Django 4.2 and we've used this utility mark_safe_lazy
from the sample in our code. This code now produces the error __str__ returned non-string (type __proxy__)
when rendering such a value in the templates.
The fix seems to simply use Django's mark_safe(gettext_lazy(...))
instead of this custom helper.
Change History (14)
comment:1 by , 13 months ago
Summary: | Docs: Sample use of lazy in delayed translations is not valid anymore → Sample use of lazy in delayed translations is not valid in Django 4.1 and 4.2. |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 13 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
follow-up: 4 comment:3 by , 13 months ago
Thanks for the information. I'm also happy that someone assigned itself to this, because this would be my first contribution and that would take some time...
But just to be sure: the sample would work in Django 5.0 due to the fix you've mentioned, but this whole docs block could actually be removed for Django >=4.1, because since then mark_safe()
preserves lazy objects and therefore a custom helper like that mark_safe_lazy()
isn't required anymore. Right?
comment:4 by , 13 months ago
Cc: | added |
---|
Replying to Stefan Hammer:
But just to be sure: the sample would work in Django 5.0 due to the fix you've mentioned, but this whole docs block could actually be removed for Django >=4.1, because since then
mark_safe()
preserves lazy objects and therefore a custom helper like thatmark_safe_lazy()
isn't required anymore. Right?
This example could stay but with a different function, or maybe it's not worth keeping 🤷 Originally it was reported for mark_safe()
. What do you think, Claude?
comment:5 by , 13 months ago
I think we might keep the section by using any string-related method of the standard lib, like isascii()
for example.
comment:6 by , 8 months ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:7 by , 5 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:9 by , 5 months ago
Patch needs improvement: | set |
---|
comment:10 by , 5 months ago
Patch needs improvement: | unset |
---|
comment:11 by , 4 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
It was fixed in Django 5.0 (check out 066aabcb77579cf8d549119c860d11cd15e3eef1). Would you like to prepare a patch? (targeted to the
stable/4.2.x
).