继承含t-name的Odoo模板失败,添加Chatter按钮报错求助
解决Odoo 17.0 Chatter顶部栏添加按钮时的AssertionError问题
错误原因
你遇到的AssertionError: Element odoo has extra content: t,是因为Odoo 17.0中继承Web组件的QWeb模板时,不能直接在<odoo>根标签下使用<t t-inherit>。必须将继承逻辑包裹在<template>标签内部,这是Odoo 17对Web组件模板结构的新要求。
修正后的XML代码
<?xml version="1.0" encoding="UTF-8"?> <odoo> <template id="inherit_chatter_top_button" inherit_id="mail.Chatter" inherit_mode="extension"> <xpath expr="//*[contains(@class, 'o-mail-Chatter-activity')]" position="after"> <button class="btn btn-secondary text-nowrap me-2"> <span>Show messages</span> </button> </xpath> </template> </odoo>
额外注意事项
- 确保你的自定义模块已依赖
mail模块,在__manifest__.py的depends列表中添加'mail'。 - 若要给按钮绑定点击交互,由于Odoo 17的Chatter是Web Component,需通过JavaScript扩展组件实现逻辑,示例如下:
import { registry } from "@web/core/registry"; import { Chatter } from "@mail/core/web/chatter"; registry.category("web_components").add("mail.Chatter", { ...Chatter, setup() { super.setup(); this.showMessages = () => { // 在此添加按钮点击逻辑 console.log("Show messages button clicked"); }; }, }); - 你的XPath表达式定位准确:Odoo 17 Chatter顶部栏的活动按钮类确实为
o-mail-Chatter-activity,按钮会被添加在活动按钮右侧。
内容的提问来源于stack exchange,提问作者Marco Lau
相关产品推荐
相关产品推荐

