我想从 Odoo 获取图像到外部网络。我尝试从 odoo 控制器获取任何 url,但没有成功。
我使用 Odoo12 作为设置值的 Web 后端,并且有一个外部网站将调用 Odoo 的 API 来使用。
在 Odoo 的数据库中,我有多个数据库,但对于这个 Web 后端,我使用一个数据库。所以我认为它必须在URL中指定数据库名称?
我尝试从 odoo12 调用 url @http.route '/web/image' 但错误“404:未找到”
请指教。预先感谢您。
在邮递员中是get
http://localhost:8069/web/image/product.template/32/image
** 响应错误。**
{
"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 \"/usr/lib/python3/dist-packages/odoo/http.py\", line 654, in _handle_exception\n return super(JsonRequest, self)._handle_exception(exception)\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 312, in _handle_exception\n raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])\n File \"/usr/lib/python3/dist-packages/odoo/tools/pycompat.py\", line 87, in reraise\n raise value\n File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 1458, in _dispatch_nodb\n func, arguments = self.nodb_routing_map.bind_to_environ(request.httprequest.environ).match()\n File \"/usr/lib/python3/dist-packages/werkzeug/routing.py\", line 1581, in match\n raise NotFound()\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
}
}
我在 Odoo12 的 Web 控制器中尝试了更多的一个 url,但对我来说都不起作用
@http.route(['/web/image',
'/web/image/<string:xmlid>',
'/web/image/<string:xmlid>/<string:filename>',
'/web/image/<string:xmlid>/<int:width>x<int:height>',
'/web/image/<string:xmlid>/<int:width>x<int:height>/<string:filename>',
'/web/image/<string:model>/<int:id>/<string:field>',
'/web/image/<string:model>/<int:id>/<string:field>/<string:filename>',
'/web/image/<string:model>/<int:id>/<string:field>/<int:width>x<int:height>',
'/web/image/<string:model>/<int:id>/<string:field>/<int:width>x<int:height>/<string:filename>',
'/web/image/<int:id>',
'/web/image/<int:id>/<string:filename>',
'/web/image/<int:id>/<int:width>x<int:height>',
'/web/image/<int:id>/<int:width>x<int:height>/<string:filename>',
'/web/image/<int:id>-<string:unique>',
'/web/image/<int:id>-<string:unique>/<string:filename>',
'/web/image/<int:id>-<string:unique>/<int:width>x<int:height>',
'/web/image/<int:id>-<string:unique>/<int:width>x<int:height>/<string:filename>'], type='http', auth="public")
def content_image(self, xmlid=None, model='ir.attachment', id=None, field='datas',
filename_field='datas_fname', unique=None, filename=None, mimetype=None,
download=None, width=0, height=0, crop=False, related_id=None, access_mode=None,
access_token=None, avoid_if_small=False, upper_limit=False, signature=False):
如果您在尝试从 Odoo 访问图像时遇到
404 Not Found
错误,常见的解决方案是确保在请求中包含会话 ID。操作方法如下:
会话 ID:您需要通过包含会话 ID 来验证您的请求。首先,登录Odoo获取会话ID。
API 调用示例:
首先,登录Odoo:
POST http://localhost:8069/web/session/authenticate 内容类型:application/json
{ “jsonrpc”:“2.0”, “参数”:{ "db": "你的数据库名称", “登录”:“您的用户名”, “密码”:“您的密码” } }
身份验证成功后,获取会话 ID 并将会话 ID 作为参数包含在图像请求中:
GET http://localhost:8069/web/image/product.template/32/image_1920?session_id=<your_session_id>
网址格式: 确保 URL 格式正确。例如:
http://localhost:8069/web/image/product.template/32/image_1920?session_id=<your_session_id>
访问图像: 通过包含会话 ID 作为参数,您的请求现在应该经过身份验证,从而允许您检索图像而不会遇到 404 错误。
事实证明,这种方法可以有效解决从 Odoo 访问图像的问题。