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

Odoo 12~15版本web controller路由冲突无额外前缀解决方案

方案2:自定义路由Converter(匹配效率更高,性能损耗更小)

如果你的自定义路径可以提前穷举,或者有固定的命名规则,可通过自定义Werkzeug转换器实现匹配阶段过滤,非目标路径直接不会进入该路由:

步骤1:定义自定义Converter

from werkzeug.routing import PathConverter
from odoo import api, SUPERUSER_ID, request

class CustomPathConverter(PathConverter):
    def to_python(self, value):
        # 这里写你的自定义路径校验规则,比如查询数据库中是否存在该路径的自定义页记录
        env = api.Environment(request.cr, SUPERUSER_ID, {})
        exists = env['your.custom.page.model'].sudo().search([('path', '=', value)], limit=1)
        if not exists:
            # 不匹配则抛出异常,路由会跳过该规则,继续匹配后续原生路由
            raise ValueError()
        return value

步骤2:注册Converter到Odoo路由系统

在模块的__init__.py中添加注册逻辑:

from odoo.http import root
from .controllers import CustomPathConverter

# 应用启动时注册自定义转换器
root.app.url_map.converters['custom_path'] = CustomPathConverter

步骤3:调整路由定义使用自定义转换器

@http.route(['/<custom_path:custom_path>/'], type='http', auth='public', website=True)
def render_new_template(self, custom_path, **post):
    # 直接写自定义逻辑即可,走到这里的路径都是符合校验规则的
    return request.render('your_module.custom_template', {'custom_path': custom_path})

适用场景:自定义路径有固定规则/可提前校验的场景,匹配阶段直接过滤无效请求,性能优于方案1


避坑提示

  • 不要直接替换Werkzeug的Map实现,会影响Odoo所有原生路由的稳定性,升级Odoo版本时也容易出现兼容性问题
  • 不要通过调整路由注册顺序解决问题,Odoo模块加载顺序不固定,后续安装新模块时可能再次出现冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 04:24:05