From 6cafd4b21f89523410c7d4c34740884c6a3552d8 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 21 Feb 2010 23:38:33 +0000 Subject: [PATCH] Fixed #7876 - Improved template error message to include expected end tag. Thanks to Matthias Kestenholz for the initial patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12460 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/__init__.py | 8 +++++--- tests/regressiontests/templates/tests.py | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/django/template/__init__.py b/django/template/__init__.py index af033222a8..584b14b06f 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -56,7 +56,7 @@ from django.template.context import Context, RequestContext, ContextPopException from django.utils.importlib import import_module from django.utils.itercompat import is_iterable from django.utils.functional import curry, Promise -from django.utils.text import smart_split, unescape_string_literal +from django.utils.text import smart_split, unescape_string_literal, get_text_list from django.utils.encoding import smart_unicode, force_unicode, smart_str from django.utils.translation import ugettext as _ from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping @@ -288,7 +288,7 @@ class Parser(object): try: compile_func = self.tags[command] except KeyError: - self.invalid_block_tag(token, command) + self.invalid_block_tag(token, command, parse_until) try: compiled_result = compile_func(self, token) except TemplateSyntaxError, e: @@ -339,7 +339,9 @@ class Parser(object): def empty_block_tag(self, token): raise self.error(token, "Empty block tag") - def invalid_block_tag(self, token, command): + def invalid_block_tag(self, token, command, parse_until=None): + if parse_until: + raise self.error(token, "Invalid block tag: '%s', expected %s" % (command, get_text_list(["'%s'" % p for p in parse_until]))) raise self.error(token, "Invalid block tag: '%s'" % command) def unclosed_block_tag(self, parse_until): diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 88afc548dc..b2ba9f6f90 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -181,6 +181,14 @@ class Templates(unittest.TestCase): settings.SETTINGS_MODULE = old_settings_module settings.TEMPLATE_DEBUG = old_template_debug + def test_invalid_block_suggestion(self): + # See #7876 + from django.template import Template, TemplateSyntaxError + try: + t = Template("{% if 1 %}lala{% endblock %}{% endif %}") + except TemplateSyntaxError, e: + self.assertEquals(e.args[0], "Invalid block tag: 'endblock', expected 'else' or 'endif'") + def test_templates(self): template_tests = self.get_template_tests() filter_tests = filters.get_filter_tests()