Opened 9 years ago

Closed 8 years ago

#14169 closed (wontfix)

Improve TemplateSyntaxError for invalid block tags

Reported by: Filip Dupanović Owned by: nobody
Component: Template system Version: master
Severity: Keywords: TemplateSyntaxError, exception value
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



{% block someblock %}{% endblock %}


{% extends 'some_template.html' %}
{% block someblock %}

{% blocktrans %}

Hello world!

{% endblocktrans%}

{% endlbock %}

Django raises a TemplateSyntaxError with an exception value "Invalid block tag: 'blocktrans', expected 'endblock' or 'endblock someblock'. Because I didn't take my morning coffee, this put me way off thinking since when was {% blocktrans %} an invalid node for {% block %}? 20 minutes later, after reverting from trunk to the v1.2 tag, I've just figured I've never loaded the {% blocktrans %} tag in the first place. Could this particular instance of TemplateSyntaxError be expanded to perform an additional check to see if sometag is actually loaded.

This would seem a more appropriate exception value:

Invalid block tag: 'blocktrans'

Attachments (1)

Django Installation Guide (1.5 KB) - added by anonymous 6 years ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 8 years ago by Matthias Kestenholz

This is hard to do, next to impossible even with the current code base.

In django.template.Parser.invalid_block_tag, we can determine which tags are allowed using self.tags. However, this dict only contains f.e. 'block', not 'endblock', only 'blocktrans', not 'endblocktrans'. We have no way of knowing whether 'endblock' or 'endblocktrans' are valid tags because the template tag author can freely choose tag names as he/she wants. It gets even worse with {% plural %} inside a blocktrans-block...

Here's my stupid attempt at a patch, if anyone wants to continue:

diff --git a/django/template/ b/django/template/
index c316786..de460d0 100644
--- a/django/template/
+++ b/django/template/
@@ -330,6 +330,8 @@ class Parser(object):
     def invalid_block_tag(self, token, command, parse_until=None):
         if parse_until:
+            if command not in self.tags:
+                raise self.error(token, "Invalid block tag: '%s', did you forget to load it?" % command)
             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)
diff --git a/tests/regressiontests/templates/ b/tests/regressiontests/templates/
index 9e2d175..3ca8bf8 100644
--- a/tests/regressiontests/templates/
+++ b/tests/regressiontests/templates/
@@ -318,6 +318,16 @@ class Templates(unittest.TestCase):
         except TemplateSyntaxError, e:
             self.assertEquals(e.args[0], "Invalid block tag: 'endblock', expected 'else' or 'endif'")
+        try:
+            t = Template("{% if 1 %}lala{% anything 'something' %}{% endif %}")
+        except TemplateSyntaxError, e:
+            self.assertEquals(e.args[0], "Invalid block tag: 'anything', did you forget to load it?")
+        try:
+            t = Template("{% load i18n %}{% if 1 %}lala{% trans 'something' %}{% bla %}{% endif %}")
+        except TemplateSyntaxError, e:
+            self.assertEquals(e.args[0], "Invalid block tag: 'bla', expected 'else' or 'endif'")
     def test_templates(self):
         template_tests = self.get_template_tests()
         filter_tests = filters.get_filter_tests()

comment:2 Changed 8 years ago by mariarchi

Resolution: wontfix
Status: newclosed

I don't really think it's feasible either

Changed 6 years ago by anonymous

Attachment: Django Installation Guide added
Note: See TracTickets for help on using tickets.
Back to Top