How to do it...

We'll need to add controllers, which go into a folder called controllers by convention:

  1. Add a controllers/main.py file with the HTML version of our page, as follows:
from odoo import http
from odoo.http import request

class Main(http.Controller):
@http.route('/my_library/books', type='http', auth='none')
def books(self):
books = request.env['library.book'].sudo().search([])
html_result = '<html><body><ul>'
for book in books:
html_result += "<li> %s </li>" % book.name
html_result += '</ul></body></html>'
return html_result
  1. Add a function to serve the same information in the JSON format, as shown in the following example:
    @http.route('/my_library/books/json', type='json', auth='none')
def books_json(self):
records = request.env['library.book'].sudo().search([])
return records.read(['name'])
  1. Add the controllers/__init__.py file, as follows:
from . import main
  1. Import the controllers to your my_library/__init__.py file, as follows:
from . import controllers

After restarting your server, you can visit /my_library/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 by using the following command to receive the output on the command-line:

curl -i -X POST -H "Content-Type: application/json" -d "{}"   
localhost:8069/my_library/books/json

If you get 404 errors at this point, you probably have more than one database available on your instance. In that case, it's impossible for Odoo to determine which database is meant to serve the request.

Use the --db-filter='^yourdatabasename$' parameter to force Odoo to use the exact database you installed the module in. The path should now be accessible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset