python - Odoo 10 : Catch the UserError and show it to webpage

标签 python error-handling controller odoo webpage

我有一个 Controller ,它在models.py中调用 action_approve 函数,最后返回一个网页:

class Holiday(http.Controller):
    @http.route('/Approve', auth='public',type='http', methods=['GET'], website=True)
    def index(self,resid):

        record = http.request.env['hr.holidays'].search([('id', '=', resid)])
        print str(record)
        result=record.action_approve()
        return http.request.render('holiday.approve_page')

这是 action_approve 函数,具有 UserError 语句:
@api.multi
    def action_approve(self):
        # if double_validation: this method is the first approval approval
        # if not double_validation: this method calls action_validate() below
        print "holiday calleds"
        if not self.env.user.has_group('hr_holidays.group_hr_holidays_manager'):
            raise UserError(_('Only Leads can approve leave requests.'))
            return False
        manager = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)

        notification=Notification()
        notification.notifyEmployee(self,'Approved')

        for holiday in self:
            if holiday.state != 'confirm':
                raise UserError(_('Leave request must be confirmed ("To Approve") in order to approve it.'))
                return False
            if holiday.double_validation:
                return holiday.write({'state': 'validate1', 'manager_id': manager.id if manager else False})
            else:
                holiday.action_validate()

我想捕获此 UserError 消息,并将其在网页上显示给用户。我怎样才能做到这一点?

最佳答案

此代码有效:

try:
            record.action_approve()

except UserError,e:
            return http.request.render('holiday.approve_page', {'errorMsg': e[0]})

关于python - Odoo 10 : Catch the UserError and show it to webpage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53121020/

相关文章:

python - aiohttp request.multipart() 从浏览器上传文件中得不到任何结果

python - Python 在添加两个变量时选择的默认值是多少?

java - post 请求成功后,postman 返回 null 值

ruby-on-rails - Omniauth Facebook 重定向到注册屏幕而不是创建用户

c++ - 漂亮的 map 打印机抛出类型错误

python - Python中多项式乘法的朴素递归算法

visual-studio - Visual Studio 2015错误: exe_main.cpp not found

python - 错误: object of type 'numpy.float64' has no len()

javascript - Promise.all 不会捕获错误

ruby-on-rails - rails : Prevent duplicate inserts due to pressing back button and save again