[Django] #25423: Using custom tag without {% load %} result in confusing traceback

40 views
Skip to first unread message

Django

unread,
Sep 17, 2015, 3:52:00 PM9/17/15
to django-...@googlegroups.com
#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.

Django

unread,
Sep 19, 2015, 8:32:02 AM9/19/15
to django-...@googlegroups.com
#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 | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:1>

Django

unread,
Sep 19, 2015, 8:34:25 AM9/19/15
to django-...@googlegroups.com
#25423: Using custom tag without {% load %} result in confusing traceback
--------------------------------------+------------------------------------
Reporter: yegle | Owner: chiller
Type: Cleanup/optimization | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by chiller):

* owner: nobody => chiller
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:2>

Django

unread,
Oct 4, 2015, 6:23:25 PM10/4/15
to django-...@googlegroups.com
#25423: Using custom tag without {% load %} result in confusing traceback
--------------------------------------+------------------------------------
Reporter: yegle | Owner: chiller
Type: Cleanup/optimization | Status: assigned
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by pindia):

* has_patch: 0 => 1


Comment:

I did some experimentation with this and it looks like it's not possible
to distinguish between a truly unknown template tag and a template tag
that isn't registered as a starting tag but might be an ending tag (like
`elif`, `endif`, etc). That's why the error message lists some "expected"
tags.

However I do have personal experience with Django users who have been very
confused by this message and didn't even think to check if they had loaded
the tag. I think your "Did you forgot to register or load this tag?"
message is a good solution. I've created a patch and PR to add that to the
error message: https://github.com/django/django/pull/5392

--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:3>

Django

unread,
Oct 5, 2015, 5:40:38 AM10/5/15
to django-...@googlegroups.com
#25423: Using custom tag without {% load %} result in confusing traceback
--------------------------------------+------------------------------------
Reporter: yegle | Owner:

Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by chiller):

* status: assigned => new
* owner: chiller =>


--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:4>

Django

unread,
Oct 5, 2015, 5:24:11 PM10/5/15
to django-...@googlegroups.com
#25423: Using custom tag without {% load %} result in confusing traceback
-------------------------------------+-------------------------------------
Reporter: yegle | Owner:
Type: | Status: new
Cleanup/optimization |

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:5>

Django

unread,
Oct 5, 2015, 5:33:14 PM10/5/15
to django-...@googlegroups.com
#25423: Using custom tag without {% load %} result in confusing traceback
-------------------------------------+-------------------------------------
Reporter: yegle | Owner: Tim
Type: | Graham <timograham@…>
Cleanup/optimization | Status: closed

Component: Template system | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* owner: => Tim Graham <timograham@…>
* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"9f2881deb127593e8e0fa25e978aad9029d7b562" 9f2881de]:
{{{
#!CommitTicketReference repository=""
revision="9f2881deb127593e8e0fa25e978aad9029d7b562"
Fixed #25423 -- Made error message for unknown template tag more helpful.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:6>

Django

unread,
Oct 5, 2015, 5:34:04 PM10/5/15
to django-...@googlegroups.com
#25423: Using custom tag without {% load %} result in confusing traceback
-------------------------------------+-------------------------------------
Reporter: yegle | Owner: Tim
Type: | Graham <timograham@…>
Cleanup/optimization | Status: closed
Component: Template system | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"3429dfe11df8c7ac59255320c6d34609570345c3" 3429dfe1]:
{{{
#!CommitTicketReference repository=""
revision="3429dfe11df8c7ac59255320c6d34609570345c3"
[1.9.x] Fixed #25423 -- Made error message for unknown template tag more
helpful.

Backport of 9f2881deb127593e8e0fa25e978aad9029d7b562 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25423#comment:7>

Reply all
Reply to author
Forward
0 new messages