URLs and Routing in the Odoo 17 Website App

CelineGeorge1 532 views 11 slides Apr 25, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

In Odoo, Controllers are used to configure front-end modules under Website. Using controllers, we can specify the URL to the link the web pages.
For that, we need to setup the controller in our module and create an xml template to load the data in the web page.


Slide Content

URLs & Routing in Odoo 17 Website App Enterprise

Introduction Enterpr ise In Odoo, Controllers are used to configure front-end modules under Website . Using controllers, we can specify the URL to the link the web pages. For that, we need to setup the controller in our module and create an xml template to load the data in the web page.

Enterprise Here, as an example just create a module named ‘sale_detail’ as follows.

Enterprise Step 1: Create a Controller in the custom module In the directory ‘Controllers’ inside the module, there are two python files (here it’s is main.py) and __init__.py. main.py have the method with which the data is taken from backend module to show in the website.

Enterprise Inside main.py, import the http and http.request and create a class to write the method. Then, use the method decorator ‘@http.route’ to navigate to a specific page using controller from odoo import http from odoo.http import request class SaleDetail(http.Controller): @http.route('/sale_detail',type='http', auth="public", website=True) def sale_detail(self, **arg): Sale_detail = request.env['sale.order'].sudo().search([]) values = {'record' : sale_detail} return request.render('sale_detail.all_sales_data', values)

Enterprise For the decorator, the first parameter ‘/sale_details’ is the URL. The second parameter says the type of the request. It can be http or json. Keep it as http. json requests are used to execute methods on server to obtain result. Type = 'http' is for responding over http request. It will take you to the another template with the values with the dictionary passed and will refresh the page Third parameter gives the authentication. The values can be public, user or none. The last parameter website=True to make it visible on website Inside the method, fetch the data we need using sudo() to get data as a super user. Here the sale order details are taken and set as a value dictionary(‘values’ here) Render the template and pass the value dictionary by calling the template with the syntax module_name. t emplate_name

Enterprise Step 2: Create the Template Inside the directory ‘views’, create a new xml file to design the template to display the data taken from backend modules. Inside <odoo> tag, give a unique name for the template. Keep in mind that the template name must be the same when we render the template from the .py file. <template id="sale_detail.all_sales_data" name="Sales Details"> Design the template by keeping Odoo’s default website’s layout using . <t t-call="website.layout">

Enterprise Design the content using the <table>, <thead>, <tbody> tags. Here, inside the div of class ‘container’, we have set a heading for the data to be shown added a table with ‘table-striped’ class as <br/> <center> <h2>SALE DETAILS</h2> </center> <table class="table-striped table"> <thead> <th>Sale Order</th> <th>Sale Customer</th> <th>Date</th> <th>Amount</th> </thead>

Enterprise T o iterate through multiple records, <t t-foreach> can be used. Here in this example, inside <tbody>, to get the details Sale order Number, Customer name, Date and Amount of each Sale order, iterate as <tbody> <t t-foreach="record" t-as="so"> <tr> <td><span t-esc="so.name"/></td> <td><span t-esc="so.partner_id.name"/></td> <td><span t-esc="so.date_order"/></td> <td><span t-esc="so.amount_total"/></td> </tr> </t> </tbody>

Enterprise Step 3: Open the URL from the website Go to the website from UI and copy our URL used in the controller, ie ‘/sale_details’ here. The page will be loaded as below with all the details we specified using the template.

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com