
本文教你如何绕过 Odoo 默认的后台管理界面(如列表页、搜索栏、新建按钮等),通过继承 web.layout 或构建独立网站主题,实现真正的“零默认视图”前端页面——仅展示自定义内容(如 Hello World、序列号生成按钮等)。
本文教你如何绕过 odoo 默认的后台管理界面(如列表页、搜索栏、新建按钮等),通过继承 `web.layout` 或构建独立网站主题,实现真正的“零默认视图”前端页面——仅展示自定义内容(如 hello world、序列号生成按钮等)。
在 Odoo 中,默认的 ir.actions.act_window 动作(如你定义的 debit_card_action)始终会渲染为后台管理视图(带菜单栏、搜索框、列表/表单控件等),这正是你看到“默认 Odoo 页面”的原因。若目标是纯粹的前端展示页(类似静态官网首页或信息看板),必须脱离 act_window 机制,改用 Odoo 的 Website 模块路由系统。
✅ 正确路径:使用 Website 模块 + 自定义 QWeb 模板
首先确保已安装 Website App(在 Apps 中搜索并安装 website)。然后在你的模块中:
-
在
manifest.py中添加依赖:'depends': ['base', 'website'], # 关键:必须依赖 website
-
创建自定义页面模板(推荐继承
web.layout):
在views/website_templates.xml中添加:<?xml version="1.0" encoding="utf-8"?><odoo><template id="debit_card_landing_page" name="Debit Card Landing Page" inherit_id="web.layout"><xpath expr="//body" position="replace"><div class="container mt-5"> <h1 class="text-center mb-4">Heinzmann Debit Card</h1> <div class="text-center mb-4"> <p>Hello World! This is a clean, standalone website page.</p> </div> <!-- 示例:调用后端方法生成序列号 --> <div class="text-center"> <button type="button" class="btn btn-primary" onclick="generateSequence()"> Generate Sequence Number </button> <div id="sequence-result" class="mt-3 text-success fw-bold"></div> </div> </div> <!-- 前端 JS 调用控制器 --> <script> function generateSequence() { fetch('/debit_card/generate_sequence', { method: 'POST', headers: {'Content-Type': 'application/json'}, credentials: 'same-origin' }) .then(r => r.json()) .then(data => { document.getElementById('sequence-result').innerText = data.sequence || 'Error'; }); } </script></xpath></template></odoo> -
添加 URL 路由控制器(Python):
在controllers/main.py中:from odoo import http from odoo.http import request class DebitCardController(http.Controller): @http.route('/debit_card/generate_sequence', type='json', auth='user', methods=['POST']) def generate_sequence(self): # 示例:简单序列号逻辑(实际可对接 ir.sequence) return {'sequence': f'DEBIT-{request.env['ir.sequence'].next_by_code('debit.card.seq') or '0001'}'}并在
__init__.py中导入该控制器。 -
注册 URL 页面(可选但推荐):
在views/website_templates.xml中追加:<template id="homepage_redirect" name="Homepage Redirect" inherit_id="website.homepage"><xpath expr="//t[@t-name='website.homepage']" position="replace"><t t-call="your_module.debit_card_landing_page"></t></xpath></template>
或直接访问
/web#action=your_module.debit_card_landing_page(需配置页面路由)。
⚠️ 注意事项与最佳实践
- ❌ 不要将
TransientModel(如debit.card)用于前端展示页——它本质是向导模型,不适合公开网页; - ✅ 若需持久化序列号,应使用
models.Model+ir.sequence,并在控制器中安全调用; - ✅ 所有前端模板必须放在
views/下,并在manifest.py的'data'列表中声明; - ✅ 使用
auth='user'确保登录态;若需完全公开,改为auth='public'并处理 CSRF(Odoo 16+ 推荐用type='http'+csrf=False配合 token); - ? 进阶需求(多页面、主题样式、SEO)请参考官方文档:Odoo Website Theming Guide。
通过以上方式,你将彻底摆脱 Odoo 后台 UI 的束缚,获得一个真正轻量、可控、可定制的前端网站页面。










