More helpful error message when passing a non-component to the component template tag

pull/7362/head
Matt Westcott 2021-08-04 17:51:37 +01:00 zatwierdzone przez Matt Westcott
rodzic e777517950
commit 8921cb3a4d
2 zmienionych plików z 13 dodań i 1 usunięć

Wyświetl plik

@ -739,7 +739,10 @@ def component(context, obj, fallback_render_method=False):
# called instead (with no arguments) - this is to provide deprecation path for things that have
# been newly upgraded to use the component pattern.
if fallback_render_method and not hasattr(obj, 'render_html') and hasattr(obj, 'render'):
has_render_html_method = hasattr(obj, 'render_html')
if fallback_render_method and not has_render_html_method and hasattr(obj, 'render'):
return obj.render()
elif not has_render_html_method:
raise ValueError("Cannot render %r as a component" % (obj,))
return obj.render_html(context)

Wyświetl plik

@ -149,3 +149,12 @@ class TestComponentTag(TestCase):
)
html = template.render(Context({'my_component': MyComponent()}))
self.assertEqual(html, "<h1>Look, I&#x27;m running with scissors! 8&lt; 8&lt; 8&lt;</h1>")
def test_error_on_rendering_non_component(self):
template = Template(
"{% load wagtailadmin_tags %}<h1>{% component my_component %}</h1>"
)
with self.assertRaises(ValueError) as cm:
template.render(Context({'my_component': "hello"}))
self.assertEqual(str(cm.exception), "Cannot render 'hello' as a component")