diff --git a/django/template/base.py b/django/template/base.py index b1eec99655c..ed1bae30224 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -122,6 +122,9 @@ class Origin: def __str__(self): return self.name + def __repr__(self): + return '<%s name=%r>' % (self.__class__.__qualname__, self.name) + def __eq__(self, other): return ( isinstance(other, Origin) and @@ -158,6 +161,12 @@ class Template: for node in self.nodelist: yield from node + def __repr__(self): + return '<%s template_string="%s...">' % ( + self.__class__.__qualname__, + self.source[:20].replace('\n', ''), + ) + def _render(self, context): return self.nodelist.render(context) diff --git a/tests/template_tests/test_base.py b/tests/template_tests/test_base.py index 475a647dd2c..2b206a89685 100644 --- a/tests/template_tests/test_base.py +++ b/tests/template_tests/test_base.py @@ -8,6 +8,17 @@ class TemplateTests(SimpleTestCase): template_string = gettext_lazy('lazy string') self.assertEqual(Template(template_string).render(Context()), template_string) + def test_repr(self): + template = Template( + '\n' + '{% if test %}

{{ varvalue }}

{% endif %}' + '' + ) + self.assertEqual( + repr(template), + '