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

Odoo16扩展ListView.buttons模板失败,无法添加列表视图顶部按钮

Odoo 16 列表视图顶部添加自定义按钮报错解决方案

错误原因

Odoo 16 对前端组件架构进行了重构,原有的 ListView.buttons 模板已被重命名为 ListView.Buttons(注意首字母大写),直接扩展旧模板名称会导致系统找不到目标模板,从而抛出Can't clone undefined template错误。

解决方法

方法一:修正模板扩展目标(快速实现)

修改你的XML模板文件,将扩展目标改为正确的ListView.Buttons,并定位到添加按钮的位置:

  1. 更新 mu_module/static/src/xml/my_buttons.xml 文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
    <t t-extend="ListView.Buttons">
        <t t-jquery=".o_list_button_add" t-operation="after">
            <button class="btn btn-primary o_list_button_jump1" type="button">Jump1</button>
        </t>
    </t>
</templates>
  1. 确保__manifest__.py的assets配置正确引入该文件,且模块依赖web:
{
    # ... 其他配置
    'depends': ['base', 'web'],
    'assets': {
        'web.assets_backend': [
            'mu_module/static/src/xml/my_buttons.xml',
        ],
    },
}

方法二:通过JS组件扩展(支持自定义逻辑)

如果需要给按钮添加点击事件等交互逻辑,推荐用组件扩展的方式:

  1. 创建JS文件 mu_module/static/src/js/list_view_buttons.js:
odoo.define('mu_module.ListViewButtons', function (require) {
    "use strict";

    const ListView = require('web.ListView');
    const ListButtons = ListView.Buttons;

    ListButtons.include({
        renderButtons() {
            this._super(...arguments);
            if (this.$buttons) {
                // 创建自定义按钮并插入到添加按钮后方
                const $jumpBtn = $('<button/>', {
                    class: 'btn btn-primary o_list_button_jump1',
                    text: 'Jump1',
                    click: () => this._onJumpBtnClick(),
                });
                this.$buttons.find('.o_list_button_add').after($jumpBtn);
            }
        },
        // 自定义按钮点击逻辑
        _onJumpBtnClick() {
            console.log('Jump1按钮被点击');
            // 这里可以添加你的业务逻辑,比如跳转、弹窗等
        },
    });

    return ListButtons;
});
  1. 在__manifest__.py中引入该JS文件:
{
    # ... 其他配置
    'depends': ['base', 'web'],
    'assets': {
        'web.assets_backend': [
            'mu_module/static/src/js/list_view_buttons.js',
        ],
    },
}

后续操作

  1. 重启Odoo服务
  2. 更新模块:执行命令 ./odoo-bin -d 你的数据库名 -u mu_module
  3. 清除浏览器缓存(Ctrl+Shift+R),刷新页面即可看到自定义按钮

内容的提问来源于stack exchange,提问作者Marko Peips

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 19:52:48