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
This commit is contained in:
Jannis Leidel 2010-02-21 23:38:33 +00:00
parent 373be1ec1f
commit 6cafd4b21f
2 changed files with 13 additions and 3 deletions

View File

@ -56,7 +56,7 @@ from django.template.context import Context, RequestContext, ContextPopException
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.itercompat import is_iterable from django.utils.itercompat import is_iterable
from django.utils.functional import curry, Promise 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.encoding import smart_unicode, force_unicode, smart_str
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
@ -288,7 +288,7 @@ class Parser(object):
try: try:
compile_func = self.tags[command] compile_func = self.tags[command]
except KeyError: except KeyError:
self.invalid_block_tag(token, command) self.invalid_block_tag(token, command, parse_until)
try: try:
compiled_result = compile_func(self, token) compiled_result = compile_func(self, token)
except TemplateSyntaxError, e: except TemplateSyntaxError, e:
@ -339,7 +339,9 @@ class Parser(object):
def empty_block_tag(self, token): def empty_block_tag(self, token):
raise self.error(token, "Empty block tag") 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) raise self.error(token, "Invalid block tag: '%s'" % command)
def unclosed_block_tag(self, parse_until): def unclosed_block_tag(self, parse_until):

View File

@ -181,6 +181,14 @@ class Templates(unittest.TestCase):
settings.SETTINGS_MODULE = old_settings_module settings.SETTINGS_MODULE = old_settings_module
settings.TEMPLATE_DEBUG = old_template_debug 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): def test_templates(self):
template_tests = self.get_template_tests() template_tests = self.get_template_tests()
filter_tests = filters.get_filter_tests() filter_tests = filters.get_filter_tests()