You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过动作按钮在Odoo框架内渲染QWeb模板?

实现思路:在Odoo框架内渲染QWeb模板

以下是几种可行方案,可让你的QWeb模板直接在Odoo后台界面中展示,而非独立网站标签页:

方案一:使用客户端动作(Client Action)

客户端动作是Odoo中加载自定义后台界面的标准方式,完全适配框架布局。

1. 调整QWeb模板适配后台样式

给模板添加Odoo后台的容器类,确保样式兼容:

<template id="test_template_backend">
    <div class="oe_content">
        <h1>My template</h1>
        <script type="text/javascript">
            console.log('it works in backend');
        </script>
        <div>
            <t t-foreach="my_items" t-as="item">
                <p t-esc="item"/>
            </t>
        </div>
    </div>
</template>

2. 定义客户端动作记录

在XML中注册客户端动作,指定关联的标识和初始数据:

<record id="action_test_template" model="ir.actions.client">
    <field name="name">Test Template</field>
    <field name="tag">test_template_client_action</field>
    <field name="context">{'my_items': [1,2,3]}</field>
</record>

3. 编写JS组件注册动作

通过Odoo的JS框架实现动作逻辑,渲染指定模板:

odoo.define('my_module.test_template_action', function (require) {
    "use strict";

    var AbstractAction = require('web.AbstractAction');
    var core = require('web.core');
    var QWeb = core.qweb;

    var TestTemplateAction = AbstractAction.extend({
        init: function(parent, context) {
            this._super(parent, context);
            this.my_items = context.my_items || [];
        },
        start: function() {
            this.$el.html(QWeb.render('my_module.test_template_backend', {
                my_items: this.my_items
            }));
            return this._super();
        }
    });

    core.action_registry.add('test_template_client_action', TestTemplateAction);
    return TestTemplateAction;
});

4. 添加菜单入口(可选)

如果需要通过菜单直接访问,关联菜单项到客户端动作:

<menuitem id="menu_test_template" name="Test Template" parent="base.menu_administration" action="action_test_template"/>

方案二:嵌入到现有业务视图

如果希望模板作为现有表单/列表/看板的一部分展示,可通过t-call指令嵌入:

示例:在伙伴表单中嵌入模板

<record id="res_partner_form_inherit" model="ir.ui.view">
    <field name="name">res.partner.form.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">
        <xpath expr="//div[@name='buttons']" position="after">
            <t t-call="my_module.test_template">
                <t t-set="my_items" t-value="[4,5,6]"/>
            </t>
        </xpath>
    </field>
</record>

方案三:自定义看板卡片视图

若要在看板中展示自定义内容,可扩展看板模板:

<record id="res_partner_kanban_inherit" model="ir.ui.view">
    <field name="name">res.partner.kanban.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.res_partner_kanban_view"/>
    <field name="arch" type="xml">
        <xpath expr="//templates/t[@t-name='kanban-box']" position="after">
            <t t-name="custom_kanban_box">
                <div class="oe_kanban_card">
                    <t t-call="my_module.test_template">
                        <t t-set="my_items" t-value="[partner.id, partner.name]"/>
                    </t>
                </div>
            </t>
        </xpath>
    </field>
</record>

关键说明

  • 客户端动作方案适合创建独立的后台界面,可通过菜单直接访问
  • 嵌入视图方案适合将模板作为现有业务流程的补充内容
  • 所有方案都支持动态获取数据(如从模型方法加载my_items,而非硬编码)

内容的提问来源于stack exchange,提问作者Wald Sin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:03:36