You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Odoo 11中Website模块index方法重写及模板加载异常问题

嘿,我来帮你搞定Odoo 11的首页替换问题!你遇到的情况我之前也碰过几次,咱们一步步排查解决:

一、先确认自定义控制器代码是否正确

Odoo 11里重写Website的首页路由,核心是把原website模块的/路由替换掉,控制器的写法一定要精准:

  • 必须继承website.controllers.main.Website
  • 路由装饰器参数要和原方法保持一致(auth="public"website=True),确保公开访问且识别网站上下文
  • 控制器文件要放在模块的controllers目录下,且__init__.py要正确导入

给你个标准的控制器示例(比如my_website/controllers/main.py):

from odoo import http
from odoo.addons.website.controllers.main import Website

class CustomWebsiteHome(Website):
    @http.route('/', type='http', auth="public", website=True)
    def index(self, **kw):
        # 渲染你的自定义模板,格式是「模块名.模板ID」
        return http.request.render('my_website.home', {
            # 传递模板需要的基础变量,比如当前网站对象
            'website': http.request.env['website'].get_current_website(),
        })
二、检查自定义模板的配置

模板没生效大概率是配置环节出了问题:

  • 模板文件要放在my_website/views/templates.xml,并且要在模块的__manifest__.pydata列表里引用这个XML文件(比如'views/templates.xml'
  • 模板结构要正确,如果你想沿用Odoo网站的头部/底部布局,记得继承website.layout,示例如下:
<odoo>
    <template id="home" name="Custom Home Page" inherit_id="website.layout">
        <xpath expr="//div[@id='wrap']" position="replace">
            <div id="wrap">
                <h1>这是我的自定义首页!</h1>
                <!-- 在这里添加你的自定义内容 -->
            </div>
        </xpath>
    </template>
</odoo>
  • 确保模板的idhome,和控制器里render'my_website.home'完全对应。
三、解决404和路由优先级问题
  • 为什么访问/index/home会404?因为你没给这两个路径配置路由啊!原website模块的首页路由只有/,如果你需要这两个路径也能访问,得在控制器里额外加路由方法:
@http.route('/home', type='http', auth="public", website=True)
def custom_home_page(self, **kw):
    return http.request.render('my_website.home', {
        'website': http.request.env['website'].get_current_website(),
    })
  • 路由优先级:一定要确保你的my_website模块在website之后加载,在__manifest__.pydepends列表里加上'website',这样Odoo会先加载官方模块,再用你的路由覆盖原有路由。
四、必做:清除缓存+升级模块

Odoo的缓存机制经常会坑人,做完上面的配置后一定要:

  1. 进入Odoo后台,找到my_website模块,点击升级
  2. 清除浏览器缓存(或者直接用无痕模式访问)
  3. 必要时重启Odoo服务,确保新代码和模板被完全加载
五、额外排查点
  • 确认my_website模块已经在后台成功安装
  • 检查controllers/__init__.py是否正确导入了控制器文件,比如:
from . import main
  • 打开Odoo调试模式,进入设置 > 技术 > 路由,搜索/,看看你的自定义路由是否在列表里,如果没有,说明控制器没被加载,要检查模块的导入和安装状态

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

火山引擎 最新活动