In Odoo 18, URLs and routing are key components of its web framework, used to handle HTTP requests. Understanding them is essential for customizing Odoo’s behavior, creating new pages, and integrating with external systems.
2. Enterprise
Introduction
In Odoo 18, URLs and routing are key components of its web framework,
used to handle HTTP requests. Understanding them is essential for
customizing Odoo’s behavior, creating new pages, and integrating with
external systems.
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.
4. Enterprise
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.
5. 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 PurchaseDetail(http.Controller):
@http.route('/purchase_detail', type='http', auth="public",
website=True)
def purchase_detail(self, **arg):
purchase_detail = request.env['purchase.order'].sudo().search([])
values = {'record' : purchase_detail}
return request.render('purchase_detail.all_purchases_data', values)
6. Enterprise
● For the decorator, the first parameter ‘/purchase_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 purchase 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. template_name
7. Enterprise
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.
● Design the template by keeping Odoo’s default website’s
layout using.
<template id="purchase_detail.all_purchases_data"
name="Purchases Details">
<t t-call="website.layout">
8. Enterprise
● Design the content using the <table>, <thead>, <tbody> tags. Here, inside the div of class
‘container’, we have to set a heading for the data to be shown, we added a table with
‘table table-striped table-dark’ class as
<br/>
<center>
<h2>PURCHASE DETAILS</h2>
</center>
<table class="table table-striped table-dark">
<thead>
<th>Purchase Order</th>
<th>Purchase Customer</th>
<th>Date</th>
<th>Amount</th>
</thead>
9. Enterprise
● To iterate through multiple records, <t t-foreach> can be used. Here in this example,
inside <tbody>, to get the details Purchase order Number, Customer name, Date and
Amount of each Purchase order, iterate as
<tbody>
<t t-foreach="record" t-as="po">
<tr>
<td><span t-esc="po.name"/></td>
<td><span t-esc="po.partner_id.name"/></td>
<td><span t-esc="po.date_order"/></td>
<td><span t-esc="po.amount_total"/></td>
</tr>
</t>
</tbody>
10. Enterprise
Open the URL from the website
Go to the website from UI and copy our URL used in the controller, ie ‘/purchase_details’ here.
The page will be loaded as below with all the details we specified using the template.
11. 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