欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Odoo Qweb页面调用python方法

程序员文章站 2024-03-24 07:58:09
...

Qweb 需要执行后台execute_code 方法:

<a t-attf-href="#{url}">
    <i t-if="menu.sys_menu.web_icon_data" t-attf-style="width: 18px;height: 18px;display: block;border: none;float: left;background-size: 18px;background-repeat: no-repeat;;background-image:url(data:image/png;base64,#{menu.sys_menu.web_icon_data});"/>
    <i t-elif="menu.web_icon_data" t-attf-style="width: 18px;height: 18px;display: block;border: none;float: left;background-size: 18px;background-repeat: no-repeat;;background-image:url(data:image/png;base64,#{menu.web_icon_data})"/>
    <i t-else="" class="icon icon-vip"/>
    <t t-if="menu.code">
        <span t-esc="execute_code(menu.code)"></span>
    </t>

</a>

在control 中增加 参数 'execute_code': self.execute_code, 传在前台。前台就可以调用了

def usercenter(self, **kwargs):
    usercenter_menu = request.env['usercenter.menu'].sudo().search([], order='display_Position,sequence')
    usercenter_menu_used = usercenter_menu
    no_footer = request.params.get('no_footer')
    no_footer = False if no_footer else True
    no_affix_top_menu = request.params.get('no_affix_top_menu')
    no_affix_top_menu = False if no_affix_top_menu else True
    values = {
        'no_footer': no_footer,
        'no_affix_top_menu': no_affix_top_menu,
        'usercenter_menu': usercenter_menu_used,
        'execute_code': self.execute_code,
        'urllib': urllib,
        'centerjscss': True
    }
    return request.render("web_user_center.miancenter", values)

@api.model
def execute_code(self, code_exec):
    localdict = {
        'cr': request.env.cr,
        'uid': request.env.uid,
        'request': request,  # 请求
        'result': None,  # 用于存储返回结果
        '_': _,
    }
    safe_eval(code_exec, localdict, mode="exec", nocopy=True)
    result = localdict['result']
    return result

 

相关标签: odoo