Django

Code

Ticket #3349 (new)

Opened 2 years ago

Last modified 5 months ago

If an ImportError occurs within some loaders a rather confusing exception is raised.

Reported by: Chris Wagner <cw264701@ohiou.edu> Assigned to: durdinator
Milestone: Component: Template system
Version: SVN Keywords:
Cc: me@andy.durdin.net Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

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. :)

Attachments

patch_3349.diff (10.4 kB) - added by durdinator on 06/05/08 11:40:40.
Updated patch against r7574 of trunk

Change History

01/22/07 16:40:37 changed by SmileyChris

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • version changed from 0.95 to SVN.
  • needs_tests changed.
  • needs_docs changed.

Thanks for the ticket, Chris. We're always willing to accept patches :)

It is a bit of an ugly error message - if you can find an elegant way around it then that'd be great.

03/02/07 03:00:16 changed by Jens Diemer

I have a similar case. The error message is misleading:

InvalidTemplateLibrary: Could not load template library from django.templatetags.pylucid_tags, No module named template

The path django.templatetags.pylucid_tags is wrong. My Module is in PyLucid.templatetags.pylucid_tags. So i have thought my Lib was not found and therefore was not loaded.

I ignored the last part of the message: No module named template. That is the actual error.

I don't know how to patch this...

(follow-up: ↓ 4 ) 03/02/07 10:18:46 changed by Chris Wagner

Jens - Do you know how to work around this problem? It doesn't actually prevent you from using template tags; it's just a really confusing error message. The problem is likely that you are trying to import a non-existent module from within your template tag library. This was what caused the error message for me. (I think that I may have even copied the culprit code from a tutorial somewhere, which didn't help me in tracking down the cause.)

(in reply to: ↑ 3 ) 05/10/07 11:41:47 changed by javinievas@gmail.com

Replying to Chris Wagner:

Jens - Do you know how to work around this problem? It doesn't actually prevent you from using template tags; it's just a really confusing error message. The problem is likely that you are trying to import a non-existent module from within your template tag library. This was what caused the error message for me. (I think that I may have even copied the culprit code from a tutorial somewhere, which didn't help me in tracking down the cause.)

After a few debugging and reversed engineering... I've found that django.templatetags.something will search something across the templatetags folder of your apps. So this is not the problem. I haven't found yet why it is happening, because I'm getting the same error and I don't have any wrong import.

05/10/07 18:17:32 changed by ubernostrum

This discussion on django-develoeprs explains a little bit of how Django loads template tag libraries, and why the error message says it's looking in django.templatetags: http://groups.google.com/group/django-developers/browse_frm/thread/f9aec919b1449539/

09/14/07 18:50:48 changed by durdinator

  • owner changed from nobody to durdinator.

09/14/07 19:41:50 changed by durdinator

  • has_patch set to 1.

This patch is a little wider in scope than the ticket. I took the core problem here and ran with it -- that an ImportError? can have various causes, and raising a different exception instead makes the traceback useless for debugging.

So this patch, in numerous places, still raises the TemplateSyntaxError? or other appropriate exception, but maintains the traceback history down to whatever caused the original exception.

This is done with a helper function, django.core.exceptions.wrap_and_raise()

09/16/07 08:03:50 changed by ubernostrum

#2298 was a duplicate.

09/17/07 05:08:21 changed by durdinator

  • cc set to me@andy.durdin.net.

06/05/08 11:40:40 changed by durdinator

  • attachment patch_3349.diff added.

Updated patch against r7574 of trunk

06/13/08 06:26:39 changed by durdinator

  • stage changed from Accepted to Ready for checkin.

06/16/08 12:29:49 changed by telenieko

  • summary changed from If an ImportError occurs within a custom template tag library, a rather confusing error message is produced to If an ImportError occurs within some loaders a rather confusing exception is raised..

Given that the patch not only affects templatetags I'm changing the summary line accordingly.

06/25/08 06:36:26 changed by russellm

  • needs_tests set to 1.
  • stage changed from Ready for checkin to Accepted.

Not ready for checkin - needs tests.


Add/Change #3349 (If an ImportError occurs within some loaders a rather confusing exception is raised.)




Change Properties
Action