Opened 7 months ago

Last modified 8 weeks ago

#34886 new Bug

Sample use of lazy in delayed translations is not valid in Django 4.1 and 4.2.

Reported by: Stefan Hammer Owned by:
Component: Documentation Version: 4.2
Severity: Normal Keywords:
Cc: Claude Paroz Triage Stage: Accepted
Has patch: no 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 (6)

comment:1 by Mariusz Felisiak, 7 months ago

Summary: Docs: Sample use of lazy in delayed translations is not valid anymoreSample use of lazy in delayed translations is not valid in Django 4.1 and 4.2.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

It was fixed in Django 5.0 (check out 066aabcb77579cf8d549119c860d11cd15e3eef1). Would you like to prepare a patch? (targeted to the stable/4.2.x).

comment:2 by SanjayBisht96, 7 months ago

Owner: changed from nobody to SanjayBisht96
Status: newassigned

comment:3 by Stefan Hammer, 7 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?

in reply to:  3 comment:4 by Mariusz Felisiak, 7 months ago

Cc: Claude Paroz 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 that mark_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 Claude Paroz, 7 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 Mariusz Felisiak, 8 weeks ago

Owner: SanjayBisht96 removed
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top