Skip to content

Commit

Permalink
Bug: Enable template tag richtext to convert lazy text strings
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed Apr 28, 2024
1 parent 7c6187f commit 0c604e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions wagtail/templatetags/wagtailcore_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.template.defaulttags import token_kwargs
from django.template.loader import render_to_string
from django.utils.encoding import force_str
from django.utils.functional import Promise
from django.utils.html import conditional_escape

from wagtail import VERSION, __version__
Expand Down Expand Up @@ -120,6 +121,8 @@ def richtext(value):
elif value is None:
html = ""
else:
if isinstance(value, Promise):
value = str(value)
if isinstance(value, str):
html = expand_db_html(value)
else:
Expand Down
5 changes: 5 additions & 0 deletions wagtail/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.test.utils import override_settings
from django.urls.exceptions import NoReverseMatch
from django.utils.safestring import SafeString
from django.utils.translation import gettext_lazy

from wagtail.coreutils import (
get_dummy_request,
Expand Down Expand Up @@ -536,6 +537,10 @@ def test_call_with_text(self):
self.assertEqual(result, "Hello world!")
self.assertIsInstance(result, SafeString)

def test_call_with_lazy(self):
result = richtext(gettext_lazy("test"))
self.assertEqual(result, "test")

def test_call_with_none(self):
result = richtext(None)
self.assertEqual(result, "")
Expand Down

0 comments on commit 0c604e6

Please sign in to comment.