Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: The richtext template tag fails on lazy strings #11900

Closed
benjaoming opened this issue Apr 28, 2024 · 0 comments
Closed

Bug: The richtext template tag fails on lazy strings #11900

benjaoming opened this issue Apr 28, 2024 · 0 comments
Labels

Comments

@benjaoming
Copy link
Contributor

Issue Summary

In an example template text like {{ page.product_warranty|default:_("No information")|richtext }}, the template code has started failing since I upgraded Wagtail.

The exception is:

'richtext' template filter received an invalid value; expected string, got <class 'django.utils.functional.lazy.<locals>.__proxy__'>.

In order to work around the issue, we can use {{ page.product_warranty|default:_("No information")|add:""|richtext }} to force a string coercion before sending to richtext.

Discussion

In the example, I'm not sure why the translation shortcut _("blah") returns a lazy evaluated text, it may be due to the nature of Template parsing and compilation.

When I fixed the issue, I quickly came across another similar issue, yet again in production. This is when I thought "oh, maybe it is feasible to fix this".

Anyways, my observation is that most errors that I've treated in Django regarding "lazy string" types happened in production, making them a bit more annoying.

I understand that the richtext filter should not treat any object like a string and needs to fail loudly in other cases. However, the objects that can be evaluated in a lazy way are perhaps fine to "guess" as strings (I didn't find a way to resolve what type of object that the lazy object resolves to). It's conventional in my experience that lazy translation objects resolve gracefully in template string filters.

Steps to Reproduce

I've provided a test case in the PR.

Technical details

I think this has happened since Wagtail 5.0.

Working on this

PR draft coming up...

@benjaoming benjaoming added status:Unconfirmed Issue, usually a bug, that has not yet been validated as a confirmed problem. type:Bug labels Apr 28, 2024
@gasman gasman closed this as completed in b266e54 Apr 29, 2024
@thibaudcolas thibaudcolas removed the status:Unconfirmed Issue, usually a bug, that has not yet been validated as a confirmed problem. label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants