From 93c312cc9cb17ec13ed1957c9a2d2e4ec56b09ed Mon Sep 17 00:00:00 2001 From: inondle Date: Fri, 27 May 2016 15:12:56 -0700 Subject: [PATCH] Fixed #26573 -- Added descriptive error message for malformed if/else/elif template tags. --- django/template/defaulttags.py | 3 ++- tests/template_tests/syntax_tests/test_if.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 5a972e3a797..41ecd0c7d2d 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -969,7 +969,8 @@ def do_if(parser, token): token = parser.next_token() # {% endif %} - assert token.contents == 'endif' + if token.contents != 'endif': + raise TemplateSyntaxError('Malformed template tag at line {0}: "{1}"'.format(token.lineno, token.contents)) return IfNode(conditions_nodelists) diff --git a/tests/template_tests/syntax_tests/test_if.py b/tests/template_tests/syntax_tests/test_if.py index 703f3d352c7..40593c7694a 100644 --- a/tests/template_tests/syntax_tests/test_if.py +++ b/tests/template_tests/syntax_tests/test_if.py @@ -508,6 +508,12 @@ class IfTagTests(SimpleTestCase): with self.assertRaises(TemplateSyntaxError): self.engine.get_template('if-tag-error12') + @setup({'else-if-tag-error01': '{% if foo is bar %} yes {% else if foo is not bar %} no {% endif %}'}) + def test_else_if_tag_error01(self): + error_message = 'Malformed template tag at line 1: "else if foo is not bar"' + with self.assertRaisesMessage(TemplateSyntaxError, error_message): + self.engine.get_template('else-if-tag-error01') + @setup({'if-tag-shortcircuit01': '{% if x.is_true or x.is_bad %}yes{% else %}no{% endif %}'}) def test_if_tag_shortcircuit01(self): """