Opened 5 years ago

Closed 4 years ago

#14169 closed (wontfix)

Improve TemplateSyntaxError for invalid block tags

Reported by: filipd 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: UI/UX:

Description

some_template.html:

{% block someblock %}{% endblock %}

some_other_template.html:

{% 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 2 years ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 4 years ago by mk

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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/__init__.py b/django/template/__init__.py
index c316786..de460d0 100644
--- a/django/template/__init__.py
+++ b/django/template/__init__.py
@@ -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/tests.py b/tests/regressiontests/templates/tests.py
index 9e2d175..3ca8bf8 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -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 4 years ago by mariarchi

  • Resolution set to wontfix
  • Status changed from new to closed

I don't really think it's feasible either

Changed 2 years ago by anonymous

Note: See TracTickets for help on using tickets.
Back to Top