How do you debug Mako templates?

后端 未结 6 1713
星月不相逢
星月不相逢 2021-01-30 21:15

So far I\'ve found it impossible to produce usable tracebacks when Mako templates aren\'t coded correctly.

Is there any way to debug templates besides iterating for ever

6条回答
  •  借酒劲吻你
    2021-01-30 22:00

    Combining the two top answers with my own special sauce:

    from flask.ext.mako import render_template as render_template_1
    from mako import exceptions
    
    app.config['MAKO_TRANSLATE_EXCEPTIONS'] = False    # seems to be necessary
    
    def render_template(*args, **kwargs):
        kwargs2 = dict(**kwargs)
        kwargs2['config'] = app.config     # this is irrelevant, but useful
        try:
            return render_template_1(*args, **kwargs2)
        except:
            if app.config.get('DEBUG'):
                return exceptions.html_error_template().render()
            raise
    

    It wraps the stock "render_template" function:

    • catch exceptions, and
      • if debugging, render a backtrace
      • if not debugging, raise the exception again so it will be logged
    • make config accessible from the page (irrelevant)

提交回复
热议问题