在odoo中创建一个控制器来处理json

问题描述 投票:0回答:2

我是oodoo的新手,并且已经使用scaffold命令创建了一个模块,如下所示:

“” C:\ Program Files(x86)\ Odoo 11.0 \ python \ python.exe“” C:\ Program Files(x86)\ Odoo 11.0 \ server \ odoo-bin“支架api4” C:\ Users \ Carlos \桌面\ custom_addons“

并且当我创建此基本重定向控制器时,它工作正常

# - * - coding: utf-8 - * -
from odoo import http
from odoo.http import request
import json
class Api4 (http.Controller):
    @ http.route ('/ api4 / api4 /', auth = 'public', website = True)
    def index (self):
        return request.redirect ('/ web /')

但是当我创建另一个@ http.route来接收json并能够处理您的数据时,它对我不起作用,而我之前完成的操作将停止工作。

    @ http.route ('/ api / json_get_request', auth = 'public', type = 'json', csrf = False)
    def jsontest (self, ** kw):
        return {'attribute': 'test'}

代码是基本的,但我想查看是否发送任何json将返回{'attribute':'test'},而是返回了此代码:

{
    "jsonrpc": "2.0",
    "id": null,
    "error": {
        "code": 404,
        "message": "404: Not Found",
        "data": {
            "name": "werkzeug.exceptions.NotFound",
            "debug": "Traceback (most recent call last): \ n File \" C: \\ Program Files (x86) \\ Odoo 11.0 \\ server \\ odoo \\ http.py \ ", line 653, in _handle_exception \ n return super (JsonRequest, self) ._ handle_exception (exception) \ n File \ "C: \\ Program Files (x86) \\ Odoo 11.0 \\ server \\ odoo \\ http.py \", line 312, in _handle_exception \ n raise pycompat.reraise (type (exception), exception, sys.exc_info () [2]) \ n File \ "C: \\ Program Files (x86) \\ Odoo 11.0 \\ server \\ odoo \\ tools \\ pycompat.py \ ", line 86, in reraise \ n raise value.with_traceback (tb) \ nwerkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. \ n ",
            "message": "404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
            "arguments": [],
            "exception_type": "internal_error"
        },
        "http_status": 404
    }
}

error postman

json controller postman odoo odoo-11
2个回答
0
投票

在odoo-bin命令中添加-d--db-filter以仅选择一个数据库。例如python3 odoo-bin --addons-path addons,mymodules -d newdatabase。据我所知,当存在多个odoo数据库时,带有auth='public'的api会引发这种错误。

替代解决方案是您可以将端点与auth='user'一起使用。您首先需要获取登录cookie。有关此的更多信息:How to connect to Odoo database from an android application


0
投票

Hello Carlos Alberto Florio Luis,

1) Clear all the cache and history in your browser.

2) keep only one database  for use and remove other databases

1) Use **--db-filter dabase-name** to load a single database.

并确保您的路线定义了没有空格选择加入('/ api / json_get_request')。

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.