Opened 13 months ago

Closed 4 months ago

Last modified 4 months ago

#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 Mariusz Felisiak, 13 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, 13 months ago

Owner: changed from nobody to SanjayBisht96
Status: newassigned

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

in reply to:  3 comment:4 by Mariusz Felisiak, 13 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, 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 Mariusz Felisiak, 8 months ago

Owner: SanjayBisht96 removed
Status: assignednew

comment:7 by Lufafa Joshua, 5 months ago

Owner: set to Lufafa Joshua
Status: newassigned

comment:8 by Lufafa Joshua, 5 months ago

Has patch: set
Last edited 5 months ago by Lufafa Joshua (previous) (diff)

comment:9 by Sarah Boyce, 5 months ago

Patch needs improvement: set

comment:10 by Lufafa Joshua, 5 months ago

Patch needs improvement: unset

comment:11 by Sarah Boyce, 4 months ago

Triage Stage: AcceptedReady for checkin

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 4 months ago

Resolution: fixed
Status: assignedclosed

In 136a5f94:

Fixed #34886 -- Modified sample use of lazy in delayed translations.

Modified example to use python standard library function
to lower the case of the string.

comment:13 by Sarah Boyce <42296566+sarahboyce@…>, 4 months ago

In c833cb3:

[5.1.x] Fixed #34886 -- Modified sample use of lazy in delayed translations.

Modified example to use python standard library function
to lower the case of the string.

Backport of 136a5f94099e428dd57572ebd16905fb25ef227e from main.

comment:14 by Sarah Boyce <42296566+sarahboyce@…>, 4 months ago

In 3a4a4497:

[5.0.x] Fixed #34886 -- Modified sample use of lazy in delayed translations.

Modified example to use python standard library function
to lower the case of the string.

Backport of 136a5f94099e428dd57572ebd16905fb25ef227e from main.

Note: See TracTickets for help on using tickets.
Back to Top