Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Mako模板继承错误TypeError:&x27;未定义';对象不可调用_Python_Mako - Fatal编程技术网

Python Mako模板继承错误TypeError:&x27;未定义';对象不可调用

Python Mako模板继承错误TypeError:&x27;未定义';对象不可调用,python,mako,Python,Mako,我有两个文件 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> &l

我有两个文件

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>
代码


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

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

请显示用于呈现模板的代码。例如,在渲染过程中传递的上下文对象是什么?请回答Thx。我删除了它和它的工作。当我检查mako文档时,我发现了链接,所以如何使用它。
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:
<td>
    ${_('User name')}
</td>
def my_func(x):
    return x

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