#3349: If an ImportError occurs within a custom template tag library, a rather
confusing error message is produced
----------------------------------------------+-----------------------------
Reporter: Chris Wagner <[EMAIL PROTECTED]> | Owner: adrian
Status: new | Component: Template system
Version: 0.95 | Keywords:
Stage: Unreviewed | Has_patch: 0
----------------------------------------------+-----------------------------
The problem is, when Django attempts to load a custom tag library (in
source:django/trunk/django/template/__init__.py, in the
{{{get_library()}}} function), it ''traps'' all {{{ImportError}}}'s,
regardless of where they are ''thrown'' from:
{{{
try:
mod = __import__(module_name, {}, {}, [''])
except ImportError, e:
raise InvalidTemplateLibrary, "Could not load template library from
%s, %s" % (module_name, e)
}}}
An example...
If I have a "view" like so:
{{{
def my_view(request):
return render_to_response('my.tpl', {})
}}}
...And a corresponding template:
{{{
{% load tag_lib %}
<h1>my template</h1>
...
}}}
...Now, here's the tag library {{{tag_lib}}}:
{{{
import notthere
...
}}}
...Invoking our view, now, will yield a not-very-easy-to-understand
(unless you already know the problem) error message:
{{{
TemplateSyntaxError at /my_view
'tag_lib' is not a valid tag library: Could not load template library from
django.templatetags.tag_lib, No module named notthere
Request Method: GET
Request URL: http://localhost:8000/my_view
Exception Type: TemplateSyntaxError
Exception Value: 'tag_lib' is not a valid tag library: Could not load
template library from django.templatetags.tag_lib, No module named
notthere
Exception Location: /usr/lib/python2.4/site-packages/Django-0.95-
py2.4.egg/django/template/defaulttags.py in load, line 692
}}}
You may need to look closely to notice that the root of the problem was
that our tag library tried to load a module {{{notthere}}}; the problem
''was not'' that we tried to load a non-existant tag library.
This is essentially the same message that you will get when trying to load
a tag library that is completely non-existant - i.e., if our template
were:
{{{
{% load not_tag_lib %}
<h1>my template</h1>
...
}}}
We would now see the error message:
{{{
TemplateSyntaxError at /my_view
'not_tag_lib' is not a valid tag library: Could not load template library
from django.templatetags.not_tag_lib, No module named not_tag_lib
...
}}}
The type of error message that I would like to see would be one that is
not obfuscated and wrapped in a {{{TemplateSyntaxError}}} exception. Such
a useful error message ''will'' be displayed if some other problem occurs
in our tag library; for instance, if our tag library looks like
{{{
blablabla
...
}}}
then we will be greeted with the slightly-more-decipherable error message,
{{{
NameError at /my_view
name 'blablabla' is not defined
Request Method: GET
Request URL: http://localhost:8000/my_view
Exception Type: NameError
Exception Value: name 'blablabla' is not defined
Exception Location: /usr/lib/python2.4/site-packages/Django-0.95-
py2.4.egg/django/template/__init__.py in get_library, line 880
}}}
I'm not certain that there's a way around this (I'm relatively new to
Python), but I suspect it is possible to resolve by (somehow) determining
which file/line the ImportError originated from/on, and adjusting the
error message that is raised in the {{{get_library()}}} function. I'd be
willing to spend time on a patch if I you're all willing to accept one. :)
--
Ticket URL: <http://code.djangoproject.com/ticket/3349>
Django Code <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---