Getting clear error messages with a Python templating system?

Tom Machinski tom.machinski at gmail.com
Mon Jul 21 18:29:27 EDT 2008


Hi there,

I'm developing web applications in Python, so I use a templating system to
produce HTML.

We're dealing with heavy traffic here, so my original choice was the fast
and efficient Cheetah. The main problem is that Cheetah doesn't provide
clear error messages. Even in the best cases, you only get some indication
of the nature of the error ("TypeError: 'str' object is not callable") but
you can't see the code where the error occurs. You can't even know the
approximate region of the template code where the problem originates.

So I tried another templating system, the newer and equally efficient Mako.
Again, same problem. It should be noted that both Cheetah and Mako compile
themselves to an intermediary Python file. This Python file is what's
actually running, so that's where the error originates. This is very
efficient, but also severely hinders debugging (see below for example of
what a very simple error on a very short Mako template generates).

So my question is: is there any way to get clear error messages with a
Python templating system?

I welcome both suggestion on how to get those with Cheetah (or Mako), and
suggestions about alternative templating systems that provide better error
messages.

Thanks,

Tom

-----

# foo.mako:
hello ${data}

>>> print mako.template.Template(filename="foo.mako").render(data="world")
hello world

>>> print
mako.template.Template(filename="foo.mako").render(dataerr="world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/usr/lib/python2.5/site-packages/Mako-0.1.10-py2.5.egg/mako/template.py",
line 114, in render
    return runtime._render(self, self.callable_, args, data)
  File
"/usr/lib/python2.5/site-packages/Mako-0.1.10-py2.5.egg/mako/runtime.py",
line 287, in _render
    _render_context(template, callable_, context, *args,
**_kwargs_for_callable(callable_, data))
  File
"/usr/lib/python2.5/site-packages/Mako-0.1.10-py2.5.egg/mako/runtime.py",
line 304, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File
"/usr/lib/python2.5/site-packages/Mako-0.1.10-py2.5.egg/mako/runtime.py",
line 337, in _exec_template
    callable_(context, *args, **kwargs)
  File "foo_mako", line 19, in render_body
  File
"/usr/lib/python2.5/site-packages/Mako-0.1.10-py2.5.egg/mako/runtime.py",
line 91, in __str__
    raise NameError("Undefined")
NameError: Undefined
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080721/487b260e/attachment.html>


More information about the Python-list mailing list