json - 在odoo中制作一个控制器来处理json
问题描述
我是 odoo 的新手,我使用脚手架命令创建了一个模块,如下所示:
"C:\Program Files (x86)\Odoo 11.0\python\python.exe" "C:\Program Files (x86)\Odoo 11.0\server\odoo-bin" 脚手架 api4"C:\Users\Carlos\Desktop\自定义插件”
当我创建这个基本重定向控制器时它工作正常
# - * - 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
}
}
解决方案
在您的 odoo-bin 命令中添加-d
或--db-filter
以仅挑选出一个数据库。例如python3 odoo-bin --addons-path addons,mymodules -d newdatabase
。据我所知,auth='public'
当有多个odoo数据库时,api会引发这种错误。
替代解决方案是您可以将端点与auth='user'
. 您需要先获取登录 cookie。更多内容:如何从 android 应用程序连接到 Odoo 数据库
推荐阅读
- python - 如何将数组中的所有对象拆分为 2 列?
- javascript - 从react Native中的userEffect中的异步函数返回一个清理函数
- javascript - javascript onClick事件找不到循环php的ID
- python - 安装pyaudio后出现ModuleNotFoundError
- bash - printf 转义码未在 Bash 脚本中格式化
- uwp - Xbox One S 启动错误:尝试从零售模式切换到开发模式时,它崩溃并出现错误系列 E200
- php - XAMPP 使用 SQLServer 更新 MSSQL 中的表,注意:数组到字符串的转换
- javascript - 如何使用 JS join() 在数组元素之前附加自定义字符串
- javascript - 访问另一个元素内的元素 JavaScript 数据
- ios - 如何在 Firebase Swift 中上传视频