Mako 模板继承错误 TypeError: 'Undefined'对象不可调用

发布于 2024-12-04 16:14:33 字数 3236 浏览 1 评论 0原文

我有 2 个文件

base_table.html table_body.html

base_table.html
<%block name="bodytable">
    <table cellspacing="0" cellpadding="0" border="1" width="100%">
        <%block name="bodytabletbody">
            <tbody>
                <%block name="innerbodytabletbody">
                    <tr>
                        <%block name="bodytableth">
                        </%block>
                    </tr>
                    <%block name="bodytablebody">
                    </%block>
                </%block>

            </tbody>
        </%block>
    </table>
</%block>

表主体继承自 base_table.html

table_body.html
<%inherit file="base_table.html">

    <%block name="bodytableth">
        <td>
            ${_('User name')}
        </td>
    </%block>

    <%block name="bodytablebody">
    </%block>


</%inherit>

当我尝试渲染它时,它给出错误

base_table_html in render_body(context, **pageargs)
base_table_html in render_bodytable(context, **pageargs)
base_table_html in render_bodytabletbody(context, **pageargs)
base_table_html in render_innerbodytabletbody(context, **pageargs)
table_body in render_bodytableth(context, **pageargs)

TypeError: 'Undefined' object is not callable

代码

# -*- coding: utf-8 -*-

import os
from mako.template import Template
from mako.lookup import TemplateLookup
from mako.runtime import Context
from cStringIO import StringIO
import tempfile
import subprocess

class MyTemplate(object):
    '''Class to get the report template object.'''

    def __init__(self):
        self.lookup = TemplateLookup(directories=['.'])


    def server_template(self, templatename):
        return self.lookup.get_template(templatename)

    def get_report(self, filename, data=None, rtype='txt'):
        '''Get the output for the given report.'''

        data = data or {}

        t1 = self.server_template(filename)

        buf = StringIO()
        ctx = Context(buf, **data)

        t1.render_context(ctx)

        report_filename = os.path.join(
                                    tempfile.gettempdir(),
                                    'test.html'
                                    )

        report_file = open(report_filename, 'w')

        report_file.write(buf.getvalue())
        report_file.close()

        if rtype == 'txt':
           report_popen = subprocess.Popen(
                                [
                                    'links', 
                                    report_filename, 
                                    '-dump', 
                                    '1'
                                ],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
           stdout, stderr = report_popen.communicate()

           return stdout

if __name__ == '__main__':
    a = MyTemplate()
    print a.get_report('table_body.html')

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

I have 2 files

base_table.html
table_body.html

base_table.html
<%block name="bodytable">
    <table cellspacing="0" cellpadding="0" border="1" width="100%">
        <%block name="bodytabletbody">
            <tbody>
                <%block name="innerbodytabletbody">
                    <tr>
                        <%block name="bodytableth">
                        </%block>
                    </tr>
                    <%block name="bodytablebody">
                    </%block>
                </%block>

            </tbody>
        </%block>
    </table>
</%block>

Table body is inherited from base_table.html

table_body.html
<%inherit file="base_table.html">

    <%block name="bodytableth">
        <td>
            ${_('User name')}
        </td>
    </%block>

    <%block name="bodytablebody">
    </%block>


</%inherit>

When i try to render it it gives error

base_table_html in render_body(context, **pageargs)
base_table_html in render_bodytable(context, **pageargs)
base_table_html in render_bodytabletbody(context, **pageargs)
base_table_html in render_innerbodytabletbody(context, **pageargs)
table_body in render_bodytableth(context, **pageargs)

TypeError: 'Undefined' object is not callable

Code

# -*- coding: utf-8 -*-

import os
from mako.template import Template
from mako.lookup import TemplateLookup
from mako.runtime import Context
from cStringIO import StringIO
import tempfile
import subprocess

class MyTemplate(object):
    '''Class to get the report template object.'''

    def __init__(self):
        self.lookup = TemplateLookup(directories=['.'])


    def server_template(self, templatename):
        return self.lookup.get_template(templatename)

    def get_report(self, filename, data=None, rtype='txt'):
        '''Get the output for the given report.'''

        data = data or {}

        t1 = self.server_template(filename)

        buf = StringIO()
        ctx = Context(buf, **data)

        t1.render_context(ctx)

        report_filename = os.path.join(
                                    tempfile.gettempdir(),
                                    'test.html'
                                    )

        report_file = open(report_filename, 'w')

        report_file.write(buf.getvalue())
        report_file.close()

        if rtype == 'txt':
           report_popen = subprocess.Popen(
                                [
                                    'links', 
                                    report_filename, 
                                    '-dump', 
                                    '1'
                                ],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
           stdout, stderr = report_popen.communicate()

           return stdout

if __name__ == '__main__':
    a = MyTemplate()
    print a.get_report('table_body.html')

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-12-11 16:14:33

看起来好像您从未将任何数据传递到模板中。在您的代码中,您的基本模板中有...

<td>
    ${_('User name')}
</td>

中间的一行基本上说,“从带有键“_”的数据中获取值,并使用“用户名”参数调用它”。似乎您没有传递任何数据,模板将尝试查找 _ 是什么,但显然不能。结果是它使用一个名为“未定义”的特殊值来表示“ <代码>_”。

由于这个“未定义”值不可调用,因此会引发错误。

要解决此问题,您需要将数据传递到模板,并且至少,它需要有一个用于“_”值的可调用对象。例如..

def my_func(x):
    return x

print a.get_report('table_body.html', data={'_' : my_func})

It looks as if you're never passing any data into your template. In your code, you have in your base template...

<td>
    ${_('User name')}
</td>

That middle line basically says, "get the value from data with the key "_", and call it with an argument of 'User name'." It seems as if you're not passing any data, the template will try to find what _ is, which it obviously can't. The result is that it uses a special value called "Undefined" for "_".

Since this "undefined" value is not callable, the error is raised.

To fix this, you need to pass data to your template, and at the very least, it needs to have a callable object for the "_" value. For example..

def my_func(x):
    return x

print a.get_report('table_body.html', data={'_' : my_func})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文