You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...
The text was updated successfully, but these errors were encountered:
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:
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...
The text was updated successfully, but these errors were encountered: