#25423: Using custom tag without {% load %} result in confusing traceback
--------------------------------------+--------------------
     Reporter:  yegle                 |      Owner:  nobody
         Type:  Cleanup/optimization  |     Status:  new
    Component:  Template system       |    Version:  1.8
     Severity:  Normal                |   Keywords:
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  0                     |      UI/UX:  0
--------------------------------------+--------------------
 In the following sample code, the author clearly registered the new tag
 but forgot to load it in template.

 {{{
 from django import template
 from django import test
 from django.template import context

 register = template.Library()

 @register.simple_tag(name="test")
 def simple_tag(*args):
   return "nothing here"

 class MyTest(test.TestCase):
   def test_forgot_to_load(self):
     TEMPLATE = """
     {% if True %}
       {% test %}
     {% endif %}
     """
     template.Template(TEMPLATE).render(context.Context())
 }}}

 This code will create the following traceback in Python 3:

 {{{

 Traceback (most recent call last):
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/base.py", line 337, in parse
     compile_func = self.tags[command]
 KeyError: 'test'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "/home/ych/testproject/testproject/test.py", line 25, in
 test_forgot_to_load
     template.Template(TEMPLATE).render(context.Context())
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/base.py", line 190, in __init__
     self.nodelist = engine.compile_string(template_string, origin)
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/engine.py", line 261, in compile_string
     return parser.parse()
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/base.py", line 341, in parse
     compiled_result = compile_func(self, token)
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/defaulttags.py", line 1014, in do_if
     nodelist = parser.parse(('elif', 'else', 'endif'))
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/base.py", line 339, in parse
     self.invalid_block_tag(token, command, parse_until)
   File "/home/ych/.py3kvenv/lib/python3.4/site-
 packages/django/template/base.py", line 394, in invalid_block_tag
     (command, get_text_list(["'%s'" % p for p in parse_until])))
 django.template.base.TemplateSyntaxError: Invalid block tag: 'test',
 expected 'elif', 'else' or 'endif'
 }}}

 And this is the traceback in Python2:


 {{{
 Traceback (most recent call last):
   File "/home/ych/testproject/testproject/test.py", line 25, in
 test_forgot_to_load
     template.Template(TEMPLATE).render(context.Context())
   File "/home/ych/.py2kvenv/local/lib/python2.7/site-
 packages/django/template/base.py", line 190, in __init__
     self.nodelist = engine.compile_string(template_string, origin)
   File "/home/ych/.py2kvenv/local/lib/python2.7/site-
 packages/django/template/engine.py", line 261, in compile_string
     return parser.parse()
   File "/home/ych/.py2kvenv/local/lib/python2.7/site-
 packages/django/template/base.py", line 341, in parse
     compiled_result = compile_func(self, token)
   File "/home/ych/.py2kvenv/local/lib/python2.7/site-
 packages/django/template/defaulttags.py", line 1014, in do_if
     nodelist = parser.parse(('elif', 'else', 'endif'))
   File "/home/ych/.py2kvenv/local/lib/python2.7/site-
 packages/django/template/base.py", line 339, in parse
     self.invalid_block_tag(token, command, parse_until)
   File "/home/ych/.py2kvenv/local/lib/python2.7/site-
 packages/django/template/base.py", line 394, in invalid_block_tag
     (command, get_text_list(["'%s'" % p for p in parse_until])))
 TemplateSyntaxError: Invalid block tag: 'test', expected 'elif', 'else' or
 'endif'
 }}}

 It says invalid block tag, but invalid how? Does that mean I should not
 use it at the specified location (which was my understanding when
 encountered this traceback), or invalid in other ways (this is the case
 here)? If invalid in other ways, how should I fix it?

 The traceback is slightly better in Python3 in that it let you know it's
 looking for 'test' in a dict but cannot find it, which will help you
 debugging this issue. But in both case it's unclear that this can be fixed
 with a simple {% load %} tag.

 Without knowing the implementation detail, I find it confusing that after
 registering the tag I still need to load it in template. If this extra
 step is unavoidable, could you please improve the error message a little
 bit? Something like:


 {{{
 Encountered unknown tag %s when parsing template %s at line %d: expect %s.
 Did you forgot to register or load this tag?
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25423>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.10f30a65f263a7f31876335ff0ff4bf4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to