First, we'll add a route that expects a traditional parameter with a book's ID to show some details about it. Then, we'll do the same, but we'll incorporate our parameter into the path itself:
- Add a path that expects a book's ID as a parameter, as shown in the following example:
@http.route('/my_library/book_details', type='http', auth='none')
def book_details(self, book_id):
record = request.env['library.book'].sudo().browse(int(book_id))
return u'<html><body><h1>%s</h1>Authors: %s' % (
record.name,
u', '.join(record.author_ids.mapped('name')) or 'none',
)
- Add a path where we can pass the book's ID in the path, as follows:
@http.route("/my_library/book_details/<model('library.book'):book>",
type='http', auth='none') def book_details_in_path(self, book): return self.book_details(book.id)
If you point your browser to /my_module/book_details?book_id=1, you should see a detailed page of the book with ID 1. If this doesn't exist, you'll receive an error page.
The second handler allows you to go to /my_module/book_details/1 and view the same page.