kopia lustrzana https://github.com/wagtail/wagtail
More helpful error message when passing a non-component to the component template tag
rodzic
e777517950
commit
8921cb3a4d
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -149,3 +149,12 @@ class TestComponentTag(TestCase):
|
|||
)
|
||||
html = template.render(Context({'my_component': MyComponent()}))
|
||||
self.assertEqual(html, "<h1>Look, I'm running with scissors! 8< 8< 8<</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")
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue