We'll need to add controllers, which go into a folder called controllers by convention:
- Add a controllers/main.py file with the HTML version of our page:
from odoo import http from odoo.http import request class Main(http.Controller): @http.route('/my_module/books', type='http', auth='none') def books(self): records = request.env['library.book'].sudo().search([]) result = '<html><body><table><tr><td>' result += '</td></tr><tr><td>'.join(records.mapped('name')) result += '</td></tr></table></body></html>' return result
- Add a function to serve the same information in the JSON format:
@http.route('/my_module/books/json', type='json', auth='none') def books_json(self): records = request.env['library.book'].sudo().search([]) return records.read(['name'])
- Add the controllers/__init__.py file:
from . import main
- Add controllers to your __init__.py addon:
from . import controllers
After restarting your server, you can visit /my_module/books in your browser and get presented with a flat list of book names. To test the JSON-RPC part, you'll have to craft a JSON request. A simple way to do that would be using the following command line to receive the output on the command line:
curl -i -X POST -H "Content-Type: application/json" -d "{}"
localhost:8069/my_module/books/json
If you get 404 errors at this point, you probably have more than one database available on your instance. In this case, it's impossible for Odoo to determine which database is meant to serve the request.
Use the --db-filter='^yourdatabasename$' parameter to force using the exact database you installed the module in. Now, the path should be accessible.